selikhovleonid/accessors-trait

属性访问器生成特质

dev-master 2017-11-30 17:51 UTC

This package is not auto-updated.

Last update: 2024-09-29 04:43:08 UTC


README

属性访问器生成特质

Latest Unstable Version License PHP from Packagist

安装

所需的最低PHP版本是PHP 5.4。您需要Composer依赖管理器来安装这个小巧的工具。

php composer.phar require selikhovleonid/accessors-trait

快速开始

当特质在子类中使用时,它会捕获该类未声明方法的调用。如果调用方法的名字符合setProperty、getProperty或isPropertySet模式,并且目标类有相应的属性,则此特质会调用所需的访问器,就像它直接在子类中声明一样。

您只需在PHPDoc块中添加以下标签之一,以标记属性可以由相应的方法访问:@get@set@isset@accessors注解将属性标记为完全可访问。

/**
 * 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();
}