模型中/** * 在初始化时进行事件绑定 */ public function init() { $this->on(self::EVENT_HELLO,[$this,'sendMail']); $this->on(self::EVENT_HELLO,[$this,'notification']); //and so on ... } public function sendMail($event) { echo 'mail sent to admin'; } public function…
To declare an event in your CComponent child class, you should add a method with aname starting with on. For example, if you add the onRegister method, you will get acorresponding event declared.A method used to declare an event becomes the defaultev…
控制器: public function actionTests1(){ $c = new \app\components\cat(); $m = new \app\components\mou; $g = new \app\components\dog; Event::on(\app\components\cat::className(),'miao',[$m,'run']); // Event::on(cat::className(),'miao',function(){echo 11;})…
根据之前一篇文章,我们知道 Yii2 的事件分两类,一是类级别的事件,二是实例级别的事件.类级别的事件是基于 yii\base\Event 实现,实例级别的事件是基于 yii\base\Component 实现. 今天先来看下类级别事件的实现,代码是 yii\base\Event 类. <?php namespace yii\base; /** * Event is the base class for all event classes. */ class Event extends Obje…