Event introduction
Using events, you can trigger the execution of a preset piece of code at a specific point in time. Events are not only a way of code decoupling, but also a way of designing business processes. model. In modern software, events are everywhere. For example, if you post a Weibo, an event will be triggered, causing people who follow you to see your new content. For events, there are several elements:
What kind of event is this? In a software system, there are many events. Publishing a new Weibo is an event, and deleting a Weibo is also an event.
Who triggered the event? What you post on Weibo is the event you trigger.
Who is responsible for monitoring this event? Or who knows if this event happened? The module on the server that handles user registration will definitely not receive your new Weibo event.
How to handle the event? For the event of publishing a new Weibo, it is to notify other users who follow you.
What is the event-related data? For publishing a new Weibo event, the data contained must at least include the content of the new Weibo, time, etc.
Case introduction: There is a cat, and the mouse will run away when it calls.
In order to implement this example, we create the event folder in the frontend folder
In# There are 2 class files in ##event folder, one Cat class and one Mouse class
<?php namespace frontend\event; /** * 猫类 * Class: \frontend\event\Cat * * 为了让猫具有事件能力 * 所以要继承 \yii\base\Component * >>> \yii\base\Component 对 \yii\base\Event 的 on 方法进行重写 * >>> \yii\base\Event 适合类级绑定 * >>> \yii\base\Component 适合对象级绑定 */ class Cat extends \yii\base\Component { /** * 猫发出叫声 */ public function shout() { echo '猫:miao miao miao <br />'; // 猫叫了之后 触发猫的 miao 事件 $this->trigger('miao'); } }
<?php namespace frontend\event; /** * 老鼠类 * Class: \frontend\event\Mouse */ class Mouse { public function run() { echo '老鼠:有猫来了,赶紧跑啊~~<br />'; } }
<?php namespace frontend\controllers; use frontend\event\Cat; use frontend\event\Mouse; /** * Class: \frontend\controllers\Event */ class EventController extends \yii\web\Controller { public function actionTest() { $cat = new Cat(); $mouse = new Mouse(); // 需事先给猫绑定 miao 事件才可以触发此事件 // 猫一叫,就触发老鼠的 run 方法 $cat->on('miao', [$mouse, 'run']); // 猫发出叫声 $cat->shout(); } }
in the browser to get
猫:miao miao miao 老鼠:有猫来了,赶紧跑啊~~
so also add the dog member Dog in the event folder
.php
<?php namespace frontend\event; /** * Class \frontend\event\Dog */ class Dog extends \yii\base\Component { /** * 找猫 */ public function findCat() { echo '狗:wang wang wang, 猫在哪里??'; } }
Add dog looking for cat event
... // 需事先给猫绑定 miao 事件才可以触发此事件 // 猫一叫,就触发老鼠的 run 方法 $cat->on('miao', [$mouse, 'run']); $cat->on('miao', [$dog, 'findCat']); // 添加狗找猫事件 // 让猫发出叫声 $cat->shout(); ...
get
猫:miao miao miao 老鼠:有猫来了,赶紧跑啊~~ 狗:wang wang wang, 猫在哪里??
Then we only need to unbind the dog looking for the cat event
Modify frontend /controllers/EventController.php
use frontend\event\Cat; use frontend\event\Mouse; use frontend\event\Dog; ... public function actionTest() { $cat = new Cat(); $mouse = new Mouse(); $dog = new Dog(); // 需事先给猫绑定 miao 事件才可以触发此事件 // 猫一叫,就触发老鼠的 run 方法 $cat->on('miao', [$mouse, 'run']); $cat->on('miao', [$dog, 'findCat']); // 并非直接删除 $cat->on('miao', [$dog, 'findCat']); // 而是通过 off 解除绑定 $cat->off('miao', [$dog, 'findCat']); // 让猫发出叫声 $cat->shout(); } ...
$cat The assigned object, is to add
(new Cat())->shout(); at the end of the actionTest method in frontend/controllers/EventController.php The miao event will not be triggered
public function actionTest() { ... // 让猫发出叫声 $cat->shout(); // 会触发 miao 事件 (new Cat())->shout(); // 不会触发 miao 事件 }
Event binding is done through the $cat objectIs there a way that the mouse will run away as long as the sound is made by the cat? Woolen cloth? ?
This requires the use of
class-level event binding
The class-level event binding requires the use of the \yii\base\Event class Modify frontend/controllers/EventController.php
use frontend\event\Cat; use frontend\event\Mouse; use yii\base\Event; ... public function actionTest() { $cat = new Cat(); $mouse = new Mouse(); // 类级别的事件绑定 // 只要猫发出声音,不管是什么猫,都会触发老鼠的 run 方法 Event::on(Cat::className() ,'miao', [$mouse, 'run']); // 让猫发出叫声 $cat->shout(); // 会触发 miao 事件 (new Cat())->shout(); // 会触发 miao 事件 }
猫:miao miao miao 老鼠:有猫来了,赶紧跑啊~~ 猫:miao miao miao 老鼠:有猫来了,赶紧跑啊~~
The above is the detailed content of Detailed explanation of examples of EVENT events in Yii2. For more information, please follow other related articles on the PHP Chinese website!