<?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 following:
*
* - 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:
* 下面是一个例子
*
* ```php
* [
* 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()
{
// Behavior 基类本身没用,主要是子类使用,重载这个函数返回一个数组表
// 示行为所关联的事件
//用于表示行为所有要响应的事件
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 ,表示这个解除绑定
}
}
}
/*
* 一个绑定了行为的类,表现起来是这样的:
*
*/
// Step 1: 定义一个将绑定行为的类
// class MyClass extends yii\base\Component
// {
// 空的
// }
// Step 2: 定义一个行为类,他将绑定到MyClass上
// class MyBehavior extends yii\base\Behavior
//{
// 行为的一个属性
// public $property1 = 'This is property in MyBehavior.';
// 行为的一个方法
// public function method1()
// {
// return 'Method in MyBehavior is called.';
// }
// }
//$myClass = new MyClass();
//$myBehavior = new MyBehavior();
// Step 3: 将行为绑定到类上
//$myClass->attachBehavior('myBehavior', $myBehavior);
// Step 4: 访问行为中的属性和方法,就和访问类自身的属性和方法一样
//echo $myClass->property1;
//echo $myClass->method1();
还没有评论,来说两句吧...