adagio / property-exposer
Give access to private properties based on phpDoc directives.
Installs: 1 151
Dependents: 0
Suggesters: 0
Security: 0
Stars: 2
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- php: ^7.0
- phpdocumentor/reflection-docblock: ^2.0|^3.0.2|^4.0
This package is auto-updated.
Last update: 2024-12-17 11:45:37 UTC
README
adagio/property-exposer
gives access to private properties based on phpDoc directives.
The goal is to avoid getters and setters in the class body and keep only meaningful action methods:
<?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() { // ... } }
Domain methods appears immediatly, easying code understanding and maintenance.
One consequence is that DTO objects have no more methods, they appears for what their are: values transporters...
Installation
The recommended way to install adagio/property-exposer
is through Composer:
composer require adagio/property-exposer
After installing, you need to require Composer's autoloader.
Usage
<?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";