aedart/athenaeum-properties

提供通过实现一些PHP的魔术方法来动态处理不可访问属性的机制


README

提供通过实现一些PHP的 魔术方法 来动态处理不可访问属性的机制。

强制使用getter-和setter方法的使用,确保如果属性确实存在,其对应的getter或setter方法将被调用。

在此上下文中,“重载”一词指的是 PHP自身的定义

示例

use Aedart\Properties\Overload;

/**
 * @property string|null $name Name of a person
 */
class Person
{
    use Overload;
    
    protected ?string $name = null;
    
    public function getName() : string
    {
	    return $this->name;
    }

    public function setName(string $value)
    {
        if(empty($value)){
            throw new InvalidArgumentException('Provided name is invalid');
        }
        
        $this->name = $value;
        
        return $this;
    }
}

在您的应用程序的其他地方,您可以调用以下内容

$person = new Person();
$person->name = 'Alin'; // Invokes the setName(...)

echo $person->name;	// Invokes the getName(), then outputs 'Alin'
echo isset($person->name); // Invokes the __isset(), then outputs true

unset($person->name); // Invokes the __unset() and destroys the name property

文档

请阅读官方文档以获取更多信息。

仓库

单仓库位于github.com/aedart/athenaeum

版本控制

此包遵循语义版本控制2.0.0

许可证

BSD-3-Clause,请阅读此包中包含的LICENSE文件