Yii2之行为
Yii三大特性:属性、事件、行为。前面两篇文章已经分别讲解了属性和事件,本文接着讲讲yii的行为,分析yii行为的实现原理。
在yii中,一个对象绑定了行为之后,就拥有了所绑定行为拥有的所有事件,而且可以访问所绑定行为的成员变量,调用其行为方法。那么,yii是怎么做到的呢?
Yii中行为的实现需要yii\base\Component和yii\base\Behavior这两个类的交互与配合,其中Component是组件类,Behavior是行为类。Component这个类在上一篇文章中讲解事件的时候已经了解了它的事件机制的实现原理,本文将解析它是如何实现行为支持的,不过在这之前先来了解一下Behavior这个行为类。话不多说,先上yii\base\Behavior类源码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | class Behavior extends Object { /** * 当前行为的拥有对象,一般为Component对象 * @var type */ public $owner ; /** * 返回当前行为类拥有的所有事件,这些事件将被attach()方法绑定到行为对象的拥有者。 * 配置格式:事件名称 => 事件处理器 * 事件处理器的配置有4种格式: * 1.当前类成员方法名称,字符串形式,相当于 [$this, '成员方法名称'] * 2.[类名, 方法名],数组形式 * 3.[对象, 方法名],数组形式 * 4.匿名函数,形式:function($event){ ... } * @return type */ public function events() { return []; } /** * 绑定行为到组件 * @param type $owner */ public function attach( $owner ) { $this ->owner = $owner ; foreach ( $this ->events() as $event => $handler ) { //遍历行为的事件列表,调用组件对象的on()方法把所有事件都绑定到组件上 $owner ->on( $event , is_string ( $handler ) ? [ $this , $handler ] : $handler ); } } /** * 从组件上解绑行为 */ public function detach() { if ( $this ->owner) { foreach ( $this ->events() as $event => $handler ) { //遍历行为的事件列表,调用组件对象的off()方法把所有事件从组件解绑 $this ->owner->off( $event , is_string ( $handler ) ? [ $this , $handler ] : $handler ); } $this ->owner = null; } } } |
可以看到yii\base\Behavior行为类其实很简单,只有一个成员变量和三个成员方法。成员变量$owner用于保存绑定当前行为的对象,这个对象所属类应该支持事件与行为,在yii中一般为Component组件类或其子类的对象,因为绑定/解绑行为的时候需要通过这个对象调用其所属类的on()/off()方法来绑定/解绑事件。成员方法events()则用于配置当前行为类所拥有的所有事件,事件处理器有4种表示方法,具体已在上面源码注释中注明。attach()和detach()两个方法分别用于绑定和解绑行为,它们分别调用$owner对象的on()和off()方法将当前行为类拥有的全部事件绑定到$owner对象或从$owner对象解绑。
我们知道,当一个Component组件对象要绑定一个Behavior行为的时候,其实主动方是这个Component组件对象,所以yii行为机制中行为的绑定和解绑都是由Component类发起的,下面就来看看Component是如何发起行为的绑定和解绑的。
先来看看Component是如何绑定行为的。Component绑定行为有两种方式:静态绑定和动态绑定,首先来了解静态绑定方法。Component类使用成员方法behaviors()来返回一个配置数组表示当前组件拥有的所有行为,使用ensureBehaviors()方法来确保这些行为都已绑定到组件上,只要访问Component的成员方法,都会先去调用ensureBehaviors()方法,若发现这些行为尚未绑定则进行绑定,所以看起来behaviors()这里配置的行为是会自动绑定的,无需我们自己写代码去绑定,所以就称之为静态绑定。behaviors()中行为的表示方法有4中方式,这里直接上源码,主要看注释:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | /** * 用于静态绑定行为 * 当访问当前组件类的属性、方法、事件的时候,都先会调用ensureBehaviors()确保这里配置的所有行为都已经绑定到组件中, * 若未绑定则一一进行绑定,这里返回的行为列表最后会保存在_behaviors成员变量中。 * @return 静态绑定到当前组件的所有行为的列表,这里的行为可以有4种表示形式: * 1. '行为类名称':匿名行为,只有行为类的名称 * 2. '行为名称' => '行为类名称':命名行为,只有行为类名称 * 3. [//匿名行为,配置数组 * 'class' => '行为类名称', * '属性名1' => '属性值1', * '属性名2' => '属性值2' * ], * 4. '行为名称' => [//命名行为,配置数组 * 'class' => '行为类名称', * '属性名1' => '属性值1', * '属性名2' => '属性值2' * ] */ public function behaviors() { return []; } public function ensureBehaviors() { if ( $this ->_behaviors === null) { $this ->_behaviors = []; foreach ( $this ->behaviors() as $name => $behavior ) { $this ->attachBehaviorInternal( $name , $behavior ); } } } |
其中ensureBehaviors()方法又调用了attachBehaviorInternal()方法,这个稍后再讲。
动态绑定行为则使用的是attachBehavior()方法,该方法源码如下:
1 2 3 4 5 6 7 8 9 10 11 | /** * 为组件绑定一个行为 * @param type $name:行为名称 * @param type $behavior:行为配置,可以是一个对象,一个行为类名,或者一个行为类对象的配置数组 * @return type */ public function attachBehavior( $name , $behavior ) { $this ->ensureBehaviors(); return $this ->attachBehaviorInternal( $name , $behavior ); } |
我们发现,不管是静态绑定还是动态绑定,最后都调用的是attachBehaviorInternal()方法,那么这个方法到底是干嘛的呢?先看源码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | /** * 为组件绑定一个行为 * @param type $name:行为名称,若是数字,表示匿名行为 * @param type $behavior:行为配置,可以是一个对象,一个行为类名,或者一个行为类对象的配置数组 * @return type */ private function attachBehaviorInternal( $name , $behavior ) { if (!( $behavior instanceof Behavior)) { //$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 ); //调用行为类对象的attach()方法,此方法将会把行为类对象的所有事件绑定到当前组件对象 $this ->_behaviors[ $name ] = $behavior ; } return $behavior ; } |
可以看到attachBehaviorInternal()方法是负责和Behavior交互的,最终调用的是Behavior类的attach()方法,上面介绍Behavior的时候已经知道attach()会调用Component类的on()方法进行事件绑定,所以,绑定了行为之后,Component就拥有了这个行为所有的事件。另外,不管是静态绑定的事件还是动态绑定的,最终Component所拥有的行为都将保存在其成员变量_behaviors中。
高度总结一下,Component绑定Behavior的过程其实是很简单的事情,只有两个步骤:
1. Component调用Behavior的attach()方法,将自身对象作为参数传递过去。
2. Behavior通过Component传递过去的对象调用Component的on()方法进行事件绑定。
讲完了Component绑定行为,接下来看看Component是如何解绑行为的。Component解绑行为的方法是detachBehavior(),源码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | /** * 从组件解绑一个行为 * @param type $name:行为名称 * @return null或解绑的行为对象 */ public function detachBehavior( $name ) { $this ->ensureBehaviors(); if (isset( $this ->_behaviors[ $name ])) { $behavior = $this ->_behaviors[ $name ]; unset( $this ->_behaviors[ $name ]); //从组件的行为列表中删除 $behavior ->detach(); //调用行为对象的detach()方法,此方法将解绑此行为绑定在组件上的所有事件 return $behavior ; } return null; } |
该方法最终调用Behavior的detach()方法,由于行为绑定的时候Behavior类的attach()方法中已经保存了行为的绑定者owner,所以解绑行为的
时候不需要传参了。在Component的detach()方法中则调用了Component类的off()方法,将行为的所有事件从组件上解绑。
这样就把yii行为的绑定和解绑原理解析完毕了。然而,文章开头说了,一个对象绑定了行为之后,不但拥有所绑定行为拥有的所有事件,而且还可以访问所绑定行为的成员变量,调用其行为方法。通过上面的讲解,我们知道Component确实拥有了Behavior的所有事件,但是没看出来Component可以访问Behavior的成员变量和方法啊。这就是魔术方法__get()、__set()以及__call()的功劳了,Component类重载了这三个方法,源码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 | /** * 重载PHP魔术方法__get()支持行为 * @param type $name:属性名称 * @return type * @throws InvalidCallException * @throws UnknownPropertyException */ public function __get( $name ) { $getter = 'get' . $name ; if (method_exists( $this , $getter )) { //当前对象存在$name属性的getter方法:直接调用 return $this -> $getter (); } $this ->ensureBehaviors(); foreach ( $this ->_behaviors as $behavior ) { //遍历当前对象已绑定的所有行为,若行为对象中存在$name属性且可读则返回 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 ); } /** * 重载PHP魔术方法__set()支持行为 * @param type $name:属性名称 * @param \yii\base\Behavior $value * @return type * @throws InvalidCallException * @throws UnknownPropertyException */ public function __set( $name , $value ) { $setter = 'set' . $name ; if (method_exists( $this , $setter )) { //当前对象存在$name属性的setter方法:直接调用 $this -> $setter ( $value ); return ; } elseif ( strncmp ( $name , 'on ' , 3) === 0) { //$name以'on '开头,表示给当前对象的某个事件绑定处理器 $this ->on(trim( substr ( $name , 3)), $value ); return ; } elseif ( strncmp ( $name , 'as ' , 3) === 0) { //$name以'as '开头,表示给当前对象绑定行为 $name = trim( substr ( $name , 3)); $this ->attachBehavior( $name , $value instanceof Behavior ? $value : Yii::createObject( $value )); return ; } $this ->ensureBehaviors(); foreach ( $this ->_behaviors as $behavior ) { //遍历当前对象已绑定的所有行为,若行为对象中存在$name属性且可写则设置值 if ( $behavior ->canSetProperty( $name )) { $behavior -> $name = $value ; return ; } } if (method_exists( $this , 'get' . $name )) { throw new InvalidCallException( 'Setting read-only property: ' . get_class( $this ) . '::' . $name ); } throw new UnknownPropertyException( 'Setting unknown property: ' . get_class( $this ) . '::' . $name ); } /** * 重写PHP魔术方法__call()支持行为, * @param type $name:方法名称 * @param type $params:传递给方法的参数 * @return type * @throws UnknownMethodException */ public function __call( $name , $params ) { $this ->ensureBehaviors(); foreach ( $this ->_behaviors as $object ) { //遍历组件绑定的所有行为,若某个行为对象中存在$name成员方法则调用之 if ( $object ->hasMethod( $name )) { return call_user_func_array([ $object , $name ], $params ); } } throw new UnknownMethodException( 'Calling unknown method: ' . get_class( $this ) . "::$name()" ); } |
这样Component绑定了Behavior之后就可以访问Behavior的成员变量,调用Behavior的成员方法了!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)