多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
#### 事件绑定 ~~~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;` 则后面的事件就都不再执行