BetterReflection\Reflection\ReflectionClass::addProperty PHP Method

addProperty() public method

Visibility defaults to \ReflectionProperty::IS_PUBLIC, or can be ::IS_PROTECTED or ::IS_PRIVATE.
public addProperty ( string $propertyName, integer $visibility = ReflectionProperty::IS_PUBLIC, boolean $static = false )
$propertyName string
$visibility integer
$static boolean
    public function addProperty($propertyName, $visibility = \ReflectionProperty::IS_PUBLIC, $static = false)
    {
        $type = 0;
        switch ($visibility) {
            case \ReflectionProperty::IS_PRIVATE:
                $type |= ClassNode::MODIFIER_PRIVATE;
                break;
            case \ReflectionProperty::IS_PROTECTED:
                $type |= ClassNode::MODIFIER_PROTECTED;
                break;
            default:
                $type |= ClassNode::MODIFIER_PUBLIC;
                break;
        }
        if ($static) {
            $type |= ClassNode::MODIFIER_STATIC;
        }
        $this->node->stmts[] = new PropertyNode($type, [new Node\Stmt\PropertyProperty($propertyName)]);
        $this->cachedProperties = null;
    }

Usage Example

 /**
  * {@inheritdoc}
  */
 public function addProperty($methodName, $visibility = 'public', $static = false)
 {
     return $this->reflectionClass->addProperty($methodName, $visibility, $static);
 }