- Contains a accessor trait which allows to register accessors
- Contains a add accessor, which means you don't have to write simple adders anymore
- Contains a get accessor, which means you don't have to write simple getters anymore
- Contains a is accessor, which means you don't have to write simple is anymore
- Contains a remove accessor, which means you don't have to write simple removers anymore
- Contains a set accessor, which means you don't have to write simple setters anymore
- PHP 5.4+
Through Composer as saxulum/saxulum-accessor.
AccessorRegistry::registerAccessor(new Add());
AccessorRegistry::registerAccessor(new Get());
AccessorRegistry::registerAccessor(new Is());
AccessorRegistry::registerAccessor(new Remove());
AccessorRegistry::registerAccessor(new Set());
/**
* @method string getName()
* @method $this setName(string $name)
* @method bool isActive()
* @method $this setActive(bool $active)
* @method $this addManies(Many $manies)
* @method Many[] getManies()
* @method $this removeManies(Many $manies)
*/
class One
{
use AccessorTrait;
/**
* @var string
*/
protected $name;
/**
* @var bool
*/
protected $active;
/**
* @var Many[]
*/
protected $manies = array();
protected function _initProps()
{
$this
->_prop((new Prop('name', Hint::STRING))
->method(Get::PREFIX)
->method(Set::PREFIX)
)
->_prop((new Prop('active', Hint::BOOL))
->method(Is::PREFIX)
->method(Set::PREFIX)
)
->_prop((new Prop('manies', 'Many[]', true, 'one', Prop::REMOTE_ONE))
->method(Add::PREFIX)
->method(Get::PREFIX)
->method(Remove::PREFIX)
)
;
}
}
/**
* @method string getName()
* @method $this setName(string $name)
* @method One getOne()
* @method $this setOne(One $name)
*/
class Many
{
use AccessorTrait;
/**
* @var string
*/
protected $name;
/**
* @var One
*/
protected $one;
protected function _initProps()
{
$this
->_prop((new Prop('name', Hint::STRING))
->method(Get::PREFIX)
->method(Set::PREFIX)
)
->_prop((new Prop('one', 'One', true, 'manies', Prop::REMOTE_MANY))
->method(Add::PREFIX)
->method(Get::PREFIX)
->method(Remove::PREFIX)
)
;
}
}
$one = new One();
$one
->setName('one')
->setActive(true)
;
$many = new Many();
$many
->setName('many')
->setOne($one)
;
$one->getName(); // return: string 'one'
$one->isActive(); // return: bool true
$one->getManies(); // return: an array with one instance of 'Many'
Call the method _generatePhpDoc
on the object using it
$one = new One();
$one->_generatePhpDoc()
- less own code to write
- less owm code to debug
- scalar type hints
- handles bidirection relations
@method
phpdoc, needs manually call_generatePhpDoc()
- slower (no benchmark)
- more complex to debug
method_exists
does not work
Yes it does, thx to __call
Does ist work with symfony/property-access (symfony/form)
Yes it does, thx to __get, __set
Does it work with twig
Yes it does, thx to the plain property method call wrapper
- Dominik Zogg dominik.zogg@gmail.com
- Dominik Zogg
- Patrick Landolt