首页 > YII 组件的事件理解

YII 组件的事件理解

<?php
/**
 * Description of TestController
 *
 * @author Administrator
 */
class TestController extends Controller{
    public function actionGet(){

          $testComponet=Yii::createComponent("application.components.TestComponent");
          $testComponet->onEcho=array($this,'kill');//register handler function to event
          $testComponet->onEcho(new CEvent($testComponet));//call this event;

    }
    public function  kill(){

        echo "kill somebody";
    }

}


我对YII中组件的事件的理解就是在一个方法中去掉用另外一个方法。只不过通过事件来做到低耦合、
不知道我的理解有没有错
判断事件是否注册了处理函数:

<?php

class TestComponent extends CComponent{
    public  $name='winner';




    public  function getName(){
        if($this->hasEventHandler('onEcho')){

            $this->onEcho(new CEvent($this));
        }




    }


    public  function  onEcho($event){
        $this->raiseEvent('onEcho', $event);

    }
}

【热门文章】
【热门文章】