wilmoore/attributes.php

PHP的最小化对象属性。

0.1.4 2014-03-30 16:51 UTC

This package is not auto-updated.

Last update: 2024-09-28 14:58:09 UTC


README

一个用于减少getter/setter模板代码的最小化特质。

特性

  • 直到需要时才省略setter/getter方法。
  • 对象的属性以JSON或数组形式表示。
  • 通过$object->firstName$object->get('firstName')获取值
  • 通过$object->firstName = 'My Name';$object->set('firstName', 'My Name')设置值
  • issetemptyunset按预期工作。
  • 定义可接受的输入值,如'seconds' => ['accepts' => '0..59']
  • 定义默认值,如'score' => ['default' => 0]

反特性

  • 依赖复杂的IDE来生成setter/getter杂项不是解决根本问题的好方法。
  • 依赖ORM不是好方法,因为不是你域中的每个对象都需要持久化。
  • 依赖基于反射的元编程。

示例

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

安装

Composer

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

要求

  • PHP 5.4+
  • [可选] PHPUnit 3.6+ 以执行测试套件 (phpunit --version)

资源

变更日志

  • (0.0.2) 20120726: 添加了Travis集成。
  • (0.0.1) 20120726: 首次可用发布。

许可证

MIT