A minimal trait to decrease getter/setter boilerplate.
- Omit setter/getter methods until needed.
- JSON or Array representation of object attributes.
- Get values via
$object->firstName
or$object->get('firstName')
- Set values via
$object->firstName = 'My Name';
or$object->set('firstName', 'My Name')
isset
,empty
, andunset
work as expected.- Define acceptable input values like
'seconds' => ['accepts' => '0..59']
- Define default values like
'score' => ['default' => 0]
- Leaning on complex IDEs to produce setter/getter cruft is not a good solution to the underlying problem.
- Leaning on an ORM is not a good solution since not every object in your domain needs to be persisted.
- Leaning on reflection-based meta-programming.
class Game {
use Attributes;
protected $__attributes = [
'gameName' => [],
'userName' => [],
'score' => ['accepts' => '0..100']
];
}
$game = new Game;
$game->set([
'gameName' => 'pacman',
'userName' => 'manny.pacquiao',
'score' => 95
]);
assert(95 === $game->score);
"require": {
"wilmoore/attributes.php": "*"
}
- PHP 5.4+
- [optional] PHPUnit 3.6+ to execute the test suite (phpunit --version)
- (0.0.2) 20120726: Added Travis Integration.
- (0.0.1) 20120726: Initial Usable Release.
MIT