- https://blog.csdn.net/qq43599939/article/details/80363827 通过观察者来理解yii事件
- 测试
class TestController extends Controller { const EVENT_MY_EVENT = 'myEvent'; public function actionTest() { echo 'this is test action<br>'; $myEvent = new MyEvent(); $this->on(self::EVENT_MY_EVENT, ['appmodulesapieventMyEvent', 'fooEvent']); //绑定事件 $this->trigger(self::EVENT_MY_EVENT, $myEvent); //触发 } } //事件类 class MyEvent extends Event { public function fooEvent() { echo 'this is my first event instance.<br>'; } }
- yii事件处理器是匿名函数,因为PHP数组可以存储匿名函数,调用可以使用call_user_func 或者 call_user_func_array 或者 $arr['func']('你试试', 'yddx'); 几种形式
<?php $arr = [ 'func' => function($p, $liupiao) { echo $p; echo $liupiao; }, 'name' => 'xixixix', ]; print_r($arr); $arr['func']('你试试', 'yddx'); call_user_func($arr['func'], 'lp', 'lp');