原文地址: http://www.cnblogs.com/dragon16/p/5532430.html
Behavior类是所有事件类的基类:
目录yii2\base\Behavior.php
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\base;
/**
* Behavior is the base class for all behavior classes.
* 所有行为的基类
* A behavior can be used to enhance the functionality of an existing component without modifying its code.
* In particular, it can "inject" its own methods and properties into the component
* and make them directly accessible via the component. It can also respond to the events triggered in the component
* and thus intercept the normal code execution.
* 用来增强现有组件的功能而不修改它的代码。它可以添加自己的方法和属性组件
* 使他们可以直接通过组件访问。还可以响应组件触发的事件,拦截正常的代码执行。
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
* 继承父类Object
*/
class Behavior extends Object
{
/**
* @var Component the owner of this behavior 要附加行为对象的组件。
*/
public $owner;
/**
* Declares event handlers for the [[owner]]'s events.
* 声明[[owner]]的事件处理程序
* Child classes may override this method to declare what PHP callbacks should
* be attached to the events of the [[owner]] component.
* 子类可以重写此方法 php回调应连接 [[owner]]组件。
* The callbacks will be attached to the [[owner]]'s events when the behavior is
* attached to the owner; and they will be detached from the events when
* the behavior is detached from the component.
* 当行为被连接到owner时回调将附在[[owner]]的事件中,当行为从组件中分离时,它们将被分离
* The callbacks can be any of the followings:
*
* - method in this behavior: `'handleClick'`, equivalent to `[$this, 'handleClick']`
* - object method: `[$object, 'handleClick']`
* - static method: `['Page', 'handleClick']`
* - anonymous function: `function ($event) { ... }`
*
* The following is an example:
*
* ~~~
* [
* Model::EVENT_BEFORE_VALIDATE => 'myBeforeValidate',
* Model::EVENT_AFTER_VALIDATE => 'myAfterValidate',
* ]
* ~~~
*
* @return array events (array keys) and the corresponding event handler methods (array values).
* 事件和相应的事件处理方法
*/
public function events()
{
return [];
}
/**
* Attaches the behavior object to the component.绑定行为到组件
* The default implementation will set the [[owner]] property
* and attach event handlers as declared in [[events]].
* 默认设置[[owner]]属性并将事件处理程序绑定到组件
* Make sure you call the parent implementation if you override this method. 如果重写方法,确保调用父类去实现
* @param Component $owner the component that this behavior is to be attached to. 行为绑定到$owner组件
*/
public function attach($owner)
{
$this->owner = $owner;//设置 $owner ,使得所依附的对象可以访问、操作
foreach ($this->events() as $event => $handler) {
//将准备响应的事件,通过所依附类的 on()方法 绑定到类上
$owner->on($event, is_string($handler) ? [$this, $handler] : $handler);
}
}
/**
* Detaches the behavior object from the component. 解除绑定的行为
* The default implementation will unset the [[owner]] property 默认取消 owner的属性
* and detach event handlers declared in [[events]]. 将events中的事件程序解除绑定
* Make sure you call the parent implementation if you override this method.如果重写方法,确保调用父类去实现
*/
public function detach()
{
if ($this->owner) {
foreach ($this->events() as $event => $handler) {//遍历行为中 events() 返回的数组
//通过Component的 off() 将绑定到类上的事件解除
$this->owner->off($event, is_string($handler) ? [$this, $handler] : $handler);
}
$this->owner = null;//将 $owner 设置为 null ,表示这个解除绑定
}
}
}