控制器:

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;});
  // $c->on('miao',[$m,'run']);
  // $c->on('miao',[$g,'look']);
  $c->shut();
}

------------------------------

<?php
namespace app\components;
use \yii\base\Event;
class myevent extends Event{
  public $message;
}
class cat extends \yii\base\Component{
  public function shut(){
    echo 'miaomiaomiao';
    $me = new myevent();
    $me->message = 'sdasdas';
    $this->trigger('miao',$me);
  }
}

-----------------------

<?php
namespace app\components;
class mou{
  public function run($me){
    echo $me->message;
    echo 'run run run';
  }
}

-----

说明:事件使用场景!在不需要返回值的场景下可使用事件进行处理!

05-08 08:44