dev-master
9999999-devTrait for Property Accessors Generation
MIT
The Requires
- php >=5.4.0
- selikhovleonid/phpdoc-parser dev-master
by Leonid Selikhov
getter setter property accessors
Trait for Property Accessors Generation
Trait for Property Accessors Generation, (*1)
The minimum required PHP version is PHP 5.4. You will need Composer dependency manager to install this tiny tool., (*3)
php composer.phar require selikhovleonid/accessors-trait
When the trait is used in a child class, it catches the calls of undeclared methods of this class. If the name of the invoked method matches the setProperty, getProperty or isPropertySet pattern and the target class has corresponding property, then this trait calls needed accessor as if it was declared directly in the child class., (*4)
You need just add one of the following tags to the PHPDoc block to mark property as
accessible to the corresponding methods: @get
, @set
, @isset
. The @accessors
annotation marks property as full-accessible., (*5)
/** * Foo class description */ class Foo { use \nadir2\tools\AccessorsTrait; /** * @var string Property description * @accessors */ protected $property; /** * @var array Another property description * @get * @isset */ private $anotherProperty = []; /** * This method sets value of 'another property'. * @param array $data Passed data. * @return self */ public function setAnotherProperty(array $data) { $this->anotherProperty = $data; return $this; } } $foo = new Foo(); // The following code is valid if (!$foo->isPropertySet()) { $foo->setProperty('bar'); $bar = $foo->getProperty(); } if (empty($foo->getAnotherProperty())) { $baz = $foo->setAnotherProperty(['baz'])->getAnotherProperty(); }
Trait for Property Accessors Generation
MIT
getter setter property accessors