概念理解:使用行为(behavior)可以在不修改现有类的情况下,对类的功能进行扩充。 通过将行为绑定到一个类,可以使类具有行为本身所定义的属性和方法,就好像类本来就有这些属性和方法一样。 而不需要写一个新的类去继承或包含现有类。在功能上类似于 Traits ,达到类似于多继承的目的。

行为的实现demo

<?phpnamespace common\components;use yii\base\Component;// 待绑定行为的类class MyClass extends Component{    }<?phpnamespace common\components;use yii\base\Behavior;// 定义一个行为类class BehaviorTest extends Behavior{    const EVENT_AFTER_SAVE = 'eventAfterAttach';    public $_val = '我是BehaviorTest里面的公有属性_val';    public function getOutput()    {        echo '我是BehaviorTest里面的公有方法getOutput';    }    public function events()    {        return [            self::EVENT_AFTER_SAVE => 'afterAttach'        ];    }    public function afterAttach()    {        echo '事件已触发';    }}// 在控制器或者命令行下调用$myClass = new MyClass();$myBehavior = new BehaviorTest();// 将行为绑定到 MyClass 的实例$myClass->attachBehavior('test', $myBehavior);// MyClass 实例调用行为类中的属性echo $myClass->_val;// MyClass 实例调用行为类中的方法$myClass->getOutput();// MyClass 实例触发行为类中定义的事件$myClass->trigger(BehaviorTest::EVENT_AFTER_SAVE);

行为的绑定原理

我们先来看一下 $myClass->attachBehavior('test', $myBehavior); 行为绑定的时候做了哪些事情?好的,又是我们的老朋友yii\base\Component

// yii\base\Component 的部分代码private $_behaviors;public function behaviors(){    return [];}public function attachBehavior($name, $behavior){        $this->ensureBehaviors();        return $this->attachBehaviorInternal($name, $behavior);}public function ensureBehaviors(){        if ($this->_behaviors === null) {            $this->_behaviors = [];            foreach ($this->behaviors() as $name => $behavior) {                $this->attachBehaviorInternal($name, $behavior);            }        }}private function attachBehaviorInternal($name, $behavior){    if (!($behavior instanceof Behavior)) {        $behavior = Yii::createObject($behavior);    }    if (is_int($name)) {        $behavior->attach($this);        $this->_behaviors[] = $behavior;    } else {        if (isset($this->_behaviors[$name])) {            $this->_behaviors[$name]->detach();        }        $behavior->attach($this);        $this->_behaviors[$name] = $behavior;    }    return $behavior;}

$myClass 调用 attachBehavior() 传入俩个参数,一个是行为名称,另一个是行为类的名称或实例或者是数组,接着 attachBehavior() 调用了 ensureBehaviors(),这个函数我们暂时用不到,因为我们没有在MyClass里面重载behaviors(),不过也能大概猜到ensureBehaviors()的用途了。再往下调用的是私有函数attachBehaviorInternal(),这个函数先判断传进来的$behavior 是否已实例化,如果还没有则进行实例化,再通过$name判断是匿名行为还是命名行为,如果是命名行为,需要查看是否已经绑定同名的行为,如果绑定了同名的行为,会将以前的行为先解绑再调用$behavior->attach($this);[注:这里$this指的MyClass实例,也就是$myClass],这样我们就来到了yii\base\Behaviorattach()方法,下面是attach()方法的源码:

public function attach($owner){    $this->owner = $owner;    foreach ($this->events() as $event => $handler) {        $owner->on($event, is_string($handler) ? [$this, $handler] : $handler);    }}

$this->owner = $owner;[注:这里$this 指的是$behavior也就是类BehaviorTest的实例],这句代码指定了行为类的主人是谁,后面的代码看着似乎似曾相识?是的,就是将行为类events()方法里面的事件也绑定的宿主身上,这里不具体展开,有兴趣的小伙伴可以看一下浅析Yii2.0的事件Event。最后将行为名称和行为实例放到$myClass的属性_behavior中,至此,行为的绑定就结束了。好像也没干什么啊,我们现在可以打印一下$myClass的数据结构是怎样的?

common\components\MyClass Object(    [_events:yii\base\Component:private] => Array        (            [eventAfterAttach] => Array                (                    [0] => Array                        (                            [0] => Array                                (                                    [0] => common\components\BehaviorTest Object                                        (                                            [_val] => 我是BehaviorTest里面的公有属性_val                                            [hello:common\components\BehaviorTest:private] => 我是BehaviorTest里面的私有属性hello                                            [owner] => common\components\MyClass Object *RECURSION*                                        )                                    [1] => afterAttach                                )                            [1] =>                         )                )        )    [_eventWildcards:yii\base\Component:private] => Array        (        )    [_behaviors:yii\base\Component:private] => Array        (            [test] => common\components\BehaviorTest Object                (                    [_val] => 我是BehaviorTest里面的公有属性_val                    [hello:common\components\BehaviorTest:private] => 我是BehaviorTest里面的私有属性hello                    [owner] => common\components\MyClass Object *RECURSION*                )        ))

可以看到$myClass已经绑定了一个行为test,绑定了一个事件eventAfterAttach,那么绑定行为以后,是怎么调用行为类里面的属性和方法呢?

行为的使用原理

还是看一下demo里面$myClass->_val这句代码是怎么执行的?根据上面$myClass的数据结构可以看出并没有_val这个属性,但是yii\base\Component里面实现了__get()这个魔术方法,我们看一下源码。

public function __get($name){    $getter = 'get' . $name;    if (method_exists($this, $getter)) {        // read property, e.g. getName()        return $this->$getter();    }    // behavior property    $this->ensureBehaviors();    foreach ($this->_behaviors as $behavior) {        if ($behavior->canGetProperty($name)) {            return $behavior->$name;        }    }    if (method_exists($this, 'set' . $name)) {        throw new InvalidCallException('Getting write-only property: ' . get_class($this) . '::' . $name);    }    throw new UnknownPropertyException('Getting unknown property: ' . get_class($this) . '::' . $name);}

又似曾相识?是的,跟yii\base\BaseObject里面属性的实现类似,有兴趣的小伙伴可以看一下浅析Yii2.0的属性Property。然后直接看注释behavior property部分,又去调用了ensureBehaviors(),先不管,接着又去遍历_behaviors这个属性,根据上面$myClass的数据结构得知,此时foreach里面的$behavior就是行为类common\components\BehaviorTest实例,先通过canGetProperty判断_val是否可读或者存在,大家可以去看yii\base\BaseObject里面该方法的实现。我们这里返回的是true,然后就直接通过common\components\BehaviorTest的实例$behavior返回_val的值。

根据上面获取行为类里面属性的流程我们注意到:

  1. 因为是通过实例化行为类去调用的属性,所以属性是protected或者是private是获取不到的。
  2. 如果Component绑定了多个行为,并且多个行为中有同名的属性,那么该Component获取的是第一个行为类里面的该属性。

那么行为类里面的方法是怎么被调用的呢?属性的调用是通过__get()来实现的,很容易联想到方法的调用是通过__call()来实现的,我们查看一下yii\base\BaseObject源码,果然里面实现__call()这个魔术方法,下面是源码,然后对照上面$myClass的数据结构一看就明白了。需要注意的是,跟上面属性的调用一样,方法也必须是public的,protected 、private方法是调用不了的。

public function __call($name, $params){    $this->ensureBehaviors();    foreach ($this->_behaviors as $object) {        if ($object->hasMethod($name)) {            return call_user_func_array([$object, $name], $params);        }    }    throw new UnknownMethodException('Calling unknown method: ' . get_class($this) . "::$name()");}

注意到__call()里面又有一个老朋友ensureBehaviors()这个函数似乎无处不在?是的,查看一下yii\base\Component里面的源码我们可以发现所有的公有方法都调用了这个函数,那么这个函数到底是干嘛的呢,其实demo里面绑定行为的方式可以称为主动绑定,就是我们主动调用函数attachBehavior()去绑定行为的,对应的就是被动绑定了,实现方式就是在待绑定行为的类里面重载behaviors()这个函数就可以实现绑定了,相当于一个行为的配置项。俩种绑定方式看个人喜好了,如果一个类需要绑定的行为很明确,推荐使用配置项的方法去绑定,也就是被动绑定。下面是将demo里面的绑定方式改成被动绑定。

<?phpnamespace common\components;use yii\base\Component;class MyClass extends Component{    public function behaviors()    {        return [            'test' => new BehaviorTest()        ];    }}此时可以将demo中 $myClass->attachBehavior('test', $myBehavior); 这句代码去掉,$myClass也是同样可以调用类 BehaviorTest 里面的属性和方法

小结

这俩天查看了Yii2.0事件、行为的实现方式,觉得有很多相似的地方,都是通过yii\base\Component来实现的,通过打印的数据结构也可以看到,Component主要就是围绕_events _eventWildcards _behaviors这三个属性展开的,其中第二个属性是 事件的通配符模式,也可以归属到 事件中,那么这样Component的主要功能就是就是实现了 事件和行为。并且实现原理上也是相似的,都是往Component里面绑定事件和行为的handle,然后触发事件或者行为的时候,再去回调相应的handle。不过在解除的时候虽然都是删掉相应的handle,但是解除行为还需要解除在绑定行为的时候绑定的事件,这点不太一样。