jclaveau / php-immutable-trait
一个用于轻松实现(不可变)对象的特性
1.0.1
2018-11-04 19:14 UTC
Requires
- php: >=5.6.0
Requires (Dev)
- dusank/knapsack: ^10.0
- jclaveau/phpunit-profile-asserts: ^1.0
- jclaveau/phpunit-testlistener-xhprof: dev-master
- jclaveau/xhprof: dev-master
- phpunit/phpunit: 5.*
This package is auto-updated.
Last update: 2024-09-08 08:06:18 UTC
README
此特性使得将实例设置为不可变或可变变得非常简单。
质量
安装
php-immutable-trait 可以通过 Composer 安装
composer require jclaveau/php-immutable-trait
用法
class ImmutableObject { use Immutable; // use SwitchableMutability; // This traits provides becomesMutable() and becomesImmutable() protected $property; public function setProperty($value) { // Just add these lines at the really beginning of methods supporting // immutability (setters mostly) if ($this->callOnCloneIfImmutable($result)) return $result; // $this is now the new instance if it's immutable $this->property = $value; return $this; } public function getProperty() { return $this->property; } } $instance = new ImmutableObject; $instance2 = $instance->setProperty('new value'); var_dump( $instance->getProperty() ); => null var_dump( $instance2->getProperty() ); => 'new value'
待办
- 配置文件
- PHP 7
- 支持私有/受保护的不可变性?开发者应该处理吗?我们应该为此提供简单的受保护API吗?