A minimal trait to decrease getter/setter boilerplate.
PHP
Switch branches/tags
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
src
test
.editorconfig
.gitignore
.travis.yml
LICENSE
README.md
composer.json
composer.lock
makefile
phpunit.xml.dist

README.md

Attributes Trait for PHP Build Status

A minimal trait to decrease getter/setter boilerplate.

Features

  • 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, and unset work as expected.
  • Define acceptable input values like 'seconds' => ['accepts' => '0..59']
  • Define default values like 'score' => ['default' => 0]

Anti-Features

  • 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.

Examples

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);

Installation

Composer

"require": {
    "wilmoore/attributes.php": "*"
}

Requirements

  • PHP 5.4+
  • [optional] PHPUnit 3.6+ to execute the test suite (phpunit --version)

Resources

Changelog

  • (0.0.2) 20120726: Added Travis Integration.
  • (0.0.1) 20120726: Initial Usable Release.

LICENSE

MIT