adagio / property-exposer
根据phpDoc指令提供对私有属性的访问。
1.2.0
2017-11-29 15:10 UTC
Requires
- php: ^7.0
- phpdocumentor/reflection-docblock: ^2.0|^3.0.2|^4.0
This package is auto-updated.
Last update: 2024-09-17 11:07:07 UTC
README
adagio/property-exposer
根据phpDoc指令提供对私有属性的访问。
目标是避免在类体内使用getter和setter,仅保留有意义的行为方法。
<?php // Before class Foo { private $bar, $baz; public function setBar($value) { $this->bar = $value; } public function getBar() { return $this->bar; } public function getBaz() { return $this->baz; } public function doSomething() { // ... } } // After /** * * @property int $bar Bar * @property-read string $baz Baz */ class Foo { use PropertyExposerTrait; private $bar, $baz; public function doSomething() { // ... } }
域方法立即出现,便于代码理解和维护。
一个后果是DTO对象不再有方法,它们仅表现为其本来的样子:值传输者...
安装
安装 adagio/property-exposer
推荐通过Composer进行。
composer require adagio/property-exposer
安装后,您需要引入Composer的自动加载器。
使用
<?php use Adagio\PropertyExposer\PropertyExposerTrait; /** * * @property int $bar Bar * @property-read string $baz Baz */ final class Foo { use PropertyExposerTrait; /** * * @var int */ private $bar = 7; /** * * @var string */ private $baz = 'test'; } $foo = new Foo; echo $foo->bar."\n"; // 7 $foo->bar = 1; echo $foo->bar."\n"; // 1 echo $foo->baz."\n"; // "test" $foo->baz = 'test2'; // Exception... echo $foo->baz."\n";