学习yii2.0框架阅读代码(十五)

组件(Component)

行为Behavior

行为是 yiiaseBehavior 或其子类的实例。行为,也称为mixins,可以无须改变类继承关系即可增强一个已有的 yiiaseComponent 类功能。当行为附加到组件后,它将“注入”它的方法和属性到组件,然后可以像访问组件内定义的方法和属性一样访问它们。此外,行为通过组件能响应被触发的事件,从而自定义或调整组件正常执行的代码。

<?php

namespace yiiase;

/**
 * 行为是所有行为类的基类.
 *
 * 一个行为可以用来增强现有的功能组件,无需修改其代码.
 * 特别是,它可以“注入”自己的方法和属性的组件
 * 通过组件,让他们直接访问。它也可以响应事件触发组件
 * 因此拦截正常的代码执行.
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
class Behavior extends Object
{
    /**
     * @var 这种行为组件的所有者
     * 指向行为本身所绑定的Component对象
     * 成员变量,用于指向行为的依附对象;
     */
    public $owner;


    /**
     * 为所有者声明事件处理程序的事件.
     *
     * Child classes may override this method to declare what PHP callbacks should
     * be attached to the events of the [[owner]] component.
     *回调将附着在所有者当行为的事件
     * 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.
     *
     * 回调函数可以是任何人:
     *
     * - method in this behavior: `'handleClick'`, equivalent to `[$this, 'handleClick']`
     * - object method: `[$object, 'handleClick']`
     * - static method: `['Page', 'handleClick']`
     * - anonymous function: `function ($event) { ... }`
     *
     * 下面是一个例子:
     *
     * ~~~
     * [
     *     Model::EVENT_BEFORE_VALIDATE => 'myBeforeValidate',
     *     Model::EVENT_AFTER_VALIDATE => 'myAfterValidate',
     * ]
     * ~~~
     *
     * 声明事件的handlers
     *
     * @return array events (array keys) and the corresponding event handler methods (array values).
     */
    public function events()
    {
        // Behavior 基类本身没用,主要是子类使用,重载这个函数返回一个数组表
        // 示行为所关联的事件
        //用于表示行为所有要响应的事件
        return [];
    }

    /**
     * 高度的行为对象组件.
     * 默认实现将所有者属性
     * 和附加事件处理程序中声明的事件.
     * 确保你调用父类实现重写这个方法.
     * 为owner添加event
     * @param Component $owner the component that this behavior is to be attached to.
     */
    public function attach($owner)
    {

        // 绑定行为到 $owner
        // 用于将行为与Component绑定起来
        $this->owner = $owner;
        foreach ($this->events() as $event => $handler) {
            $owner->on($event, is_string($handler) ? [$this, $handler] : $handler);
        }
    }

    /**
     * 分离对象的组件的行为.
     * The default implementation will unset the [[owner]] property
     * and detach event handlers declared in [[events]].
     * Make sure you call the parent implementation if you override this method.
     * 移除owner的event,并将owner置空
     */
    public function detach()
    {
        // 用于将行为从Component上解除
        if ($this->owner) {
            foreach ($this->events() as $event => $handler) {
                $this->owner->off($event, is_string($handler) ? [$this, $handler] : $handler);
            }
            $this->owner = null;
        }
    }
}

 一个绑定了行为的类,表现起来是这样的:

// Step 1: 定义一个将绑定行为的类
class MyClass extends yiiaseComponent
{
    // 空的
}

// Step 2: 定义一个行为类,他将绑定到MyClass上
class MyBehavior extends yiiaseBehavior
{
    // 行为的一个属性
    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();
原文地址:https://www.cnblogs.com/xwzj/p/5437063.html