ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
#### 事件绑定 ~~~php use lying\db\ActiveRecord; use lying\service\Event; Event::on(ActiveRecord::class, ActiveRecord::EVENT_BEFORE_DELETE, function (Event $event) { var_dump($event->_sender); }, '绑定时的额外数据'); //事件默认是插入到队列的结尾的,如果要把事件插入到队列首位 Event::on(ActiveRecord::class, ActiveRecord::EVENT_BEFORE_DELETE, function (Event $event) { var_dump($event->_sender); }, '绑定时的额外数据', false); ~~~ #### 触发事件 ~~~php use lying\db\ActiveRecord; use lying\service\Event; Event::trigger(ActiveRecord::class, ActiveRecord::EVENT_BEFORE_DELETE); Event::trigger(ActiveRecord::class, ActiveRecord::EVENT_BEFORE_DELETE, new Event()); ~~~ #### 卸载事件 ~~~php use lying\db\ActiveRecord; use lying\service\Event; $call = function (Event $event) { } //卸载指定事件 Event::off(ActiveRecord::class, ActiveRecord::EVENT_BEFORE_DELETE, $call); //卸载所有事件 Event::off(ActiveRecord::class, ActiveRecord::EVENT_BEFORE_DELETE); ~~~ ### 总结 * 事件的触发顺序是和绑定顺序一致的 * 如果有一个事件的函数返回了false或者设置`$event->stop = false;` 则后面的事件就都不再执行