1234567891011121314151617181920212223242526272829 |
- <?php
- trait Events {
- private $events = [];
- public function on(string $name, Callable $fn){
- if(!isset($this->events[$name])){
- $this->events[$name] = [];
- }
- $this->events[$name][] = $fn;
- return $this;
- }
- public function off(string $name, Callable $fn = null){
- if(isset($this->events[$name])){
- $a = $this->events[$name];
- foreach($a as $k => $f){
- if(is_null($fn) || $f == $fn){
- array_splice($a, $k, 1);
- }
- }
- }
- }
- public function fire(string $name, ...$args){
- if(isset($this->events[$name])){
- foreach($this->events[$name] as $fn){
- $fn(...$args);
- }
- }
- }
- }
- ?>
|