marcopetersen / php-chain
1.0.3
2017-03-03 04:09 UTC
Requires
- php: >=5.6
Requires (Dev)
- phpunit/phpunit: ^5.7.15
This package is not auto-updated.
Last update: 2024-09-26 14:22:03 UTC
README
PHP中实现责任链模式的示例。
安装
composer require marcopetersen/php-chain
用法
<?php use MarcoPetersen\Chain\Chain; use MarcoPetersen\Chain\Link; // First we define the links that will be part of our chain... class AddOne extends Link { public function execute($number) { return $this->next($number + 1); } } class EndChain extends Link { public function execute($payload) { return $payload; } } // ...after which we chain them up together. $chain = (new Chain()) ->then(new AddOne()) // you can pass in instances... ->then(AddOne::class) // ...or just the FQCN, if you prefer. ->then(EndChain::class) // To end the chain, just don't call `next`. ->then(AddOne::class) // This won't get called. $chain->execute(1); // 3