Detailed explanation of examples of EVENT events in Yii2

黄舟
Release: 2023-03-14 22:32:01
Original
1874 people have browsed it

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.

Code implementation

Object level binding

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 &#39;猫:miao miao miao <br />&#39;;
        
        // 猫叫了之后 触发猫的 miao 事件
        $this->trigger(&#39;miao&#39;);
    }
}
Copy after login

Mouse.php

<?php

namespace frontend\event;

/**
 * 老鼠类
 * Class: \frontend\event\Mouse
 */
class Mouse
{
    public function run()
    {
        echo &#39;老鼠:有猫来了,赶紧跑啊~~<br />&#39;;
    }
}
Copy after login

EventController.php

<?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(&#39;miao&#39;, [$mouse, &#39;run&#39;]);

        // 猫发出叫声
        $cat->shout();
    }
}
Copy after login

Enter http://yourdomain.com/?r=event/test

in the browser to get

猫:miao miao miao 
老鼠:有猫来了,赶紧跑啊~~
Copy after login

Trigger the miao event by calling the cat's shout method, The mouse ran away

Suddenly, one day, the dog joined this case. As long as the cat barks, the dog will go to the cat

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 &#39;狗:wang wang wang, 猫在哪里??&#39;;
    }
}
Copy after login

Modify frontend/controllers/EventController.php

Add dog looking for cat event

...
// 需事先给猫绑定 miao 事件才可以触发此事件
// 猫一叫,就触发老鼠的 run 方法
$cat->on(&#39;miao&#39;, [$mouse, &#39;run&#39;]);
$cat->on(&#39;miao&#39;, [$dog, &#39;findCat&#39;]); // 添加狗找猫事件

// 让猫发出叫声
$cat->shout();
...
Copy after login

Refresh http://yourdomain.com/?r=event/ in the browser test

get

猫:miao miao miao 
老鼠:有猫来了,赶紧跑啊~~
狗:wang wang wang, 猫在哪里??
Copy after login

Suddenly, the dog feels bored and doesn’t want to look for the cat anymore. It just barks.

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(&#39;miao&#39;, [$mouse, &#39;run&#39;]);
    $cat->on(&#39;miao&#39;, [$dog, &#39;findCat&#39;]);

    // 并非直接删除 $cat->on(&#39;miao&#39;, [$dog, &#39;findCat&#39;]);
    // 而是通过 off 解除绑定
    $cat->off(&#39;miao&#39;, [$dog, &#39;findCat&#39;]);

    // 让猫发出叫声
    $cat->shout();
}
...
Copy after login

So the final result is naturally less dog sound

Class level binding

But there is a problem, the above events are directly targeted

$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 事件
}
Copy after login

Reason:

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() ,&#39;miao&#39;, [$mouse, &#39;run&#39;]);

    // 让猫发出叫声
    $cat->shout(); // 会触发 miao 事件
    (new Cat())->shout(); // 会触发 miao 事件
}
Copy after login

Refresh the page and get

猫:miao miao miao 
老鼠:有猫来了,赶紧跑啊~~
猫:miao miao miao 
老鼠:有猫来了,赶紧跑啊~~
Copy after login

Summary

    ##Event binding classification object level and class level binding Defined
  • The object level is only effective for a certain instantiated object
  • The class level is effective for all instantiated objects
  • If there are any errors in the above understanding, please feel free to raise them and correct them

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template