首页 > 开发 > Php > 正文

深入解析PHP的Yii框架中的event事件机制

2020-02-21 20:47:23
字体:
来源:转载
供稿:网友

事件
事件可以将自定义代码“注入”到现有代码中的特定执行点。附加自定义代码到某个事件,当这个事件被触发时,这些代码就会自动执行。例如,邮件程序对象成功发出消息时可触发 messageSent 事件。如想追踪成功发送的消息,可以附加相应追踪代码到messageSent 事件。
Yii 引入了名为 yii/base/Component 的基类以支持事件。如果一个类需要触发事件就应该继承 yii/base/Component 或其子类。

Yii的event机制
YII的事件机制,是其比较独特之处,合理使用好事件机制,会使各个组件之间的耦合更为松散,利于团体协作开发。
何时需要使用事件,如何给事件绑定事件处理函数,以及如何触发事件,与其它语言是有较大的差别的。例如Javascript中,可以使用

$(‘#id').on("click",function() {});

方式给DOM元素绑定处理函数,当DOM元素上发生指定的事件(如click)时,将自动执行设定的函数。
但是PHP是服务器端的脚本语言,就不存在自动触发事件之说,所以和Javascript对比,YII中的事件是需要手动触发的。一般来说,要实现YII组件的事件机制,需要以下几步:

定义事件名称,其实就是级组件定义一个on开头的方法,其中的代码是固定的,如:

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

即函数名与事件名是一致的。此步的作用就是将绑定在此事件上的处理函数逐个执行。写这一系列的播客,算是一个整理,所以我写细一点,现在把raiseEvent方法的代码贴出来。

/** * Raises an event.   * This method represents the happening of an event. It invokes   * all attached handlers for the event.   * @param string $name the event name   * @param CEvent $event the event parameter   * @throws CException if the event is undefined or an event handler is invalid. */  public function raiseEvent($name,$event){         $name=strtolower($name);         //_e这个数组用来存所有事件信息       if(isset($this->_e[$name]))  {             foreach($this->_e[$name] as $handler) {                 if(is_string($handler))               call_user_func($handler,$event);                   elseif(is_callable($handler,true)){                  if(is_array($handler)){                           // an array: 0 - object, 1 - method name                     list($object,$method)=$handler;                      if(is_string($object)) // static method call                     call_user_func($handler,$event);                    elseif(method_exists($object,$method))                          $object->$method($event);                           else                              throw new CException(Yii::t('yii','Event "{class}.{event}" is attached with an invalid handler "{handler}".',  array('{class}'=>get_class($this), '{event}'=>$name, '{handler}'=>$handler[1])));                        }                        else // PHP 5.3: anonymous function                      call_user_func($handler,$event);                 }                 else                    throw new CException(Yii::t('yii','Event "{class}.{event}" is attached with an invalid handler "{handler}".', array('{class}'=>get_class($this), '{event}'=>$name, '{handler}'=>gettype($handler))));                // stop further handling if param.handled is set true             if(($event instanceof CEvent) && $event->handled)               return;             }         }  elseif(YII_DEBUG && !$this->hasEvent($name))             throw new CException(Yii::t('yii','Event "{class}.{event}" is not defined.',     array('{class}'=>get_class($this), '{event}'=>$name))); }            
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表