serafim / immutable
1.0.0
2019-11-22 01:59 UTC
Requires
- php: ^7.2|^8.0
Requires (Dev)
- phpunit/phpunit: ^7.2|^8.0
- sensiolabs/security-checker: ^6.0
- squizlabs/php_codesniffer: ^3.5
- symfony/var-dumper: ~4.3|~5.0
- vimeo/psalm: ^3.6
This package is auto-updated.
Last update: 2020-08-09 01:56:39 UTC
README
安装
通过 Composer 安装
composer require serafim/immutable
使用方法
为确保对象的不可变性,只需将您的任何方法代码包裹在一个闭包中即可。
可变对象示例
class Example { private int $value = 42; public function update(int $newValue): self { $this->value = $newValue; return $this; } }
使其不可变
class Example { private int $value = 42; // Sample #1 (PHP 7.4+) public function with(int $newValue): self { return immutable(fn () => $this->value = $newValue); } // Sample #2 (PHP 7.3 and below) public function with(int $newValue): self { return immutable(function (): void { $this->value = $newValue; }); } }
就这些了!
功能
- ☑ 完全兼容 PHP 7.2, PHP 7.3, PHP 7.4 和 PHP 8.0 (dev) (证据)
- ☑ PSR-4
- ☑ PSR-12
- ☑ 方便的助手,不会破坏代码 =)
- ☑ 感受不可变对象的强大力量!
许可
查看 LICENSE