php yii框架原理,Yii框架组件的事件机制原理与用法分析

本文实例讲述了Yii框架组件的事件机制原理与用法。分享给大家供大家参考,具体如下:

在深入分析 Yii 的运行之前,我们先来看一下 Yii 框架中一个很重要的机制 - 事件。

Yii 官方参考文档关于组件事件的解释:

=======================================================================

组件事件是一些特殊的属性,它们使用一些称作 事件句柄 ( event handlers )的方法作为其值。 附加 ( 分配 ) 一个方法到一个事件将会引起方法在事件被唤起处自动被调用。因此, 一个组件的行为可能会被一种在部件开发过程中不可预见的方式修改。

组件事件以 on 开头的命名方式定义。和属性通过 getter/setter 方法来定义的命名方式一样, 事件的名称是大小写不敏感的。以下代码定义了一个 onClicked 事件 :

public function onClicked($event)

{

$this->raiseEvent('onClicked', $event);

}

这里作为事件参数的 $event 是 CEvent或其子类的实例。

我们可以附加一个方法到此 event ,如下所示 :

$component->onClicked=$callback;

这里的 $callback 指向了一个有效的 PHP 回调。它可以是一个全局函数也可以是类中的一个方法。 如果是后者,它必须以一个数组的方式提供 : array($object,'methodName').

事件句柄的结构如下:

function methodName($event)

{

......

}

这里的 $event 即描述事件的参数(它来源于 raiseEvent() 调用)。 $event 参数是 CEvent或其子类的实例。 至少,它包含了关于谁触发了此事件的信息。

从版本 1.0.10 开始,事件句柄也可以是一个 PHP 5.3 以后支持的匿名函数。例如,

$component->onClicked=function($event) {

......

}

如果我们现在调用 onClicked() , onClicked 事件将被触发(在 onClicked() 中), 附属的事件句柄将被自动调用。

一个事件可以绑定多个句柄。当事件触发时, 这些句柄将被按照它们绑定到事件时的顺序依次执行。如果句柄决定组织后续句柄被执行,它可以设置 $event->handled为 true 。

=======================================================================

从这一句开始”我们可以附加一个方法到此 event “,读者可能 就不知道是什么意思了,于是看一下 CComponent 的源码:

/**

* Raises an event.

* This method represents the happening of an event. It invokes

* all attached handlers for the event.

* @param string the event name

* @param CEvent the event parameter

* @throws CException if the event is undefined or an event handler is invalid.

*/

public function raiseEvent($name,$event)

{

//事件名称同一小写化处理

$name=strtolower($name);

//先查看成员变量是否有以此命名的事件

if(isset($this->_e[$name]))

{

//如果有,这个成员保存的是每一个事件处理器

//以数组的方式保存

foreach($this->_e[$name] as $handler)

{

//如果事件处理器是一个字符串,那么就是一个全局函数

if(is_string($handler))

call_user_func($handler,$event);

//如果不是,那么有可能是一个数组,该数组包含一个对象和方法名

//参考http://php.net/manual/en/function.is-callable.php

else if(is_callable($handler,true))

{

// an array: 0 - object, 1 - method name

list($object,$method)=$handler;

//如果对象是一个对象名

if(is_string($object)) // static method call

call_user_func($handler,$event);

//判断对象是否有要调用的方法

else if(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

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

//如果想停止继续循环获取事件的handler

//那么需要设置event的handled为true

if(($event instanceof CEvent) && $event->handled)

return;

}

}

else if(YII_DEBUG && !$this->hasEvent($name))

throw new CException(Yii::t('yii','Event "{class}.{event}" is not defined.',

array('{class}'=>get_class($this), '{event}'=>$name)));

//如果_e中没有这个成员也没关系

}

我们再看一下 CEvent 的代码( CComponent.php ):

class CEvent extends CComponent

{

/**

* @var object the sender of this event

*/

public $sender;

/**

* @var boolean whether the event is handled. Defaults to false.

* When a handler sets this true, the rest uninvoked handlers will not be invoked anymore.

*/

public $handled=false;

/**

* Constructor.

* @param mixed sender of the event

*/

public function __construct($sender=null)

{

$this->sender=$sender;

}

}

CEvent 只包含两个变量 $sender 记录事件触发者, $handled 表示事件是否已经被“解决”。

接着我们再看一下如何给一个组件注册一个事件处理器:

/**

* Attaches an event handler to an event.

*

* An event handler must be a valid PHP callback, i.e., a string referring to

* a global function name, or an array containing two elements with

* the first element being an object and the second element a method name

* of the object.

*

* An event handler must be defined with the following signature,

*

 
 

* function handlerName($event) {}

*

* where $event includes parameters associated with the event.

*

* This is a convenient method of attaching a handler to an event.

* It is equivalent to the following code:

*

 
 

* $component->getEventHandlers($eventName)->add($eventHandler);

*

*

* Using {@link getEventHandlers}, one can also specify the excution order

* of multiple handlers attaching to the same event. For example:

*

 
 

* $component->getEventHandlers($eventName)->insertAt(0,$eventHandler);

*

* makes the handler to be invoked first.

*

* @param string the event name

* @param callback the event handler

* @throws CException if the event is not defined

* @see detachEventHandler

*/

public function attachEventHandler($name,$handler)

{

$this->getEventHandlers($name)->add($handler);

}

/**

* Returns the list of attached event handlers for an event.

* @param string the event name

* @return CList list of attached event handlers for the event

* @throws CException if the event is not defined

*/

public function getEventHandlers($name)

{

if($this->hasEvent($name))

{

$name=strtolower($name);

if(!isset($this->_e[$name]))

//新建一个CList保存事件的处理器

$this->_e[$name]=new CList;

return $this->_e[$name];

}

else

throw new CException(Yii::t('yii','Event "{class}.{event}" is not defined.',

array('{class}'=>get_class($this), '{event}'=>$name)));

}

由此可以看出,首先获取事件处理器对象,如果没有则使用 CList ( Yii 实现的一个链表)创建,然后将事件处理器 add 进这个对象中,这样就可以在 raiseEvent 时遍历所有的事件处理器进行处理了,有点儿类似 jQuery 中注册了多个 click 事件处理器之后,当 click 事件触发时,会按顺序调用之前注册的事件处理器。

希望本文所述对大家基于Yii框架的PHP程序设计有所帮助。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值