php-libs / value-states
扩展了php-libs/observer以跟踪值是否已初始化或修改(初始化后更改)
1.3
2021-05-19 07:16 UTC
Requires
- php: ^8.0
- php-libs/observable: ^1.3
README
此包包含一组简单的类,用于简化将观察者添加到属性方法的操作。
特性
-
在值更改前执行
callable -
在特定值更改前执行
callable -
在值更改后执行
callable -
在特定值更改后执行
callable
快速入门
安装
composer require php-libs/observable
无观察属性示例类
class MyClass { private ?string $propertyA = null; private ?string $propertyB = null; public function setPropertyA(string $value) { $this->propertyA = $value; } public function setPropertyB(string $value) { $this->propertyB = $value; } }
具有观察属性示例类
class MyClass implements \PhpLibs\Observable\BeforeValueChangeObservableInterface { private const PROPERTY_A = 'propertyA'; private const PROPERTY_B = 'propertyB'; use PhpLibs\Observable\BeforeValueChangeObservableTrait; use PhpLibs\Observable\AfterValueChangeObservableTrait; private ?string $propertyA = null; private ?string $propertyB = null; public function setPropertyA(string $value) { $this->raiseBeforeValueChange(static::PROPERTY_A, $this->propertyA, $value); $this->propertyA = $value; $this->raiseAfterValueChange(static::PROPERTY_A, $this->propertyA); } public function setPropertyB(string $value) { $this->raiseBeforeValueChange(static::PROPERTY_B, $this->propertyB, $value); $this->propertyB = $value; $this->raiseAfterValueChange(static::PROPERTY_B, $this->propertyB); } }
完整示例
请参阅example.php
输出如下
propertyA WILL CHANGE from "" to "A-1"
propertyB WILL CHANGE from "" to "B-1"
Begin observing After B Changed
propertyA WILL CHANGE from "A-1" to "A-2"
propertyB WILL CHANGE from "B-1" to "B-2"
propertyB CHANGED to "B-2"
End observing After B Changed
propertyA WILL CHANGE from "A-2" to "A-3"
propertyB WILL CHANGE from "B-2" to "B-3"