组件(component),是Yii框架的基类,实现了属性、事件、行为三类功能,如果需要事件和行为的功能,需要继承该类。
yii\base\Component代码详解
1 <?php 2 /** 3 * @link http://www.yiiframework.com/ 4 * @copyright Copyright (c) 2008 Yii Software LLC 5 * @license http://www.yiiframework.com/license/ 6 */ 7 8 namespace yii\base; 9 //命名空间 10 use Yii; 11 //加载Yii相关类 12 /** 13 * Component is the base class that implements the *property*, *event* and *behavior* features. 14 * 组件是实现属性、事件、行为的基类 15 * Component provides the *event* and *behavior* features, in addition to the *property* feature which is implemented in 16 * its parent class [[Object]]. 17 * 组件提供事件和行为,继承他父类Object的特点 18 * Event is a way to "inject" custom code into existing code at certain places. For example, a comment object can trigger 19 * an "add" event when the user adds a comment. We can write custom code and attach it to this event so that when the event 20 * is triggered (i.e. comment will be added), our custom code will be executed. 21 * 22 * An event is identified by a name that should be unique within the class it is defined at. Event names are *case-sensitive*. 23 * 事件是一种在特定的地方“注入”自定义代码到现有的代码。例如, 24 * 当一个用户添加评论事件时,注释的对象可以触发。我们可以编写自定义代码,并将其附加到此事件,这样当事件被触发(即评论将被添加), 25 * 26 * 我们的自定义代码会被执行, 27 * One or multiple PHP callbacks, called *event handlers*, can be attached to an event. You can call [[trigger()]] to 28 * raise an event. When an event is raised, the event handlers will be invoked automatically in the order they were 29 * attached. 30 * 31 * To attach an event handler to an event, call [[on()]]: 32 * 一个或多个php回调,称为为‘事件处理’。要触发一个事件处理,可以用on();举个例子。 33 * ~~~ 34 * $post->on('update', function ($event) { 35 * // send email notification 36 * }); 37 * ~~~ 38 * 39 * In the above, an anonymous function is attached to the "update" event of the post. You may attach 40 * the following types of event handlers: 41 * 一个匿名函数被连接到'update'事件后。可以用以下类型的程序做处理: 42 * - anonymous function: `function ($event) { ... }` 匿名函数 43 * - object method: `[$object, 'handleAdd']` 对象方法 44 * - static class method: `['Page', 'handleAdd']` 静态类方法 45 * - global function: `'handleAdd'` 全局函数 46 * 47 * The signature of an event handler should be like the following: 48 * 一个事件处理程序的签名应该是如下: 49 * ~~~ 50 * function foo($event) 51 * ~~~ 52 * 53 * where `$event` is an [[Event]] object which includes parameters associated with the event. 54 * $event是一个[[Event]]对象 包括与事件相关联的参数。 55 * You can also attach a handler to an event when configuring a component with a configuration array. 56 * The syntax is like the following: 57 * 还可以将处理程序附加到配置数组中配置组件的事件。语法如下: 58 * ~~~ 59 * [ 60 * 'on add' => function ($event) { ... } 61 * ] 62 * ~~~ 63 * 64 * where `on add` stands for attaching an event to the `add` event. 65 * `on add` 代表添加事件 66 * Sometimes, you may want to associate extra data with an event handler when you attach it to an event 67 * and then access it when the handler is invoked. You may do so by 68 * 有时,当你将它附加到一个事件时,你可能会把额外的数据与事件处理程序关联起来 69 * 然后在调用处理程序时访问它。 70 * ~~~ 71 * $post->on('update', function ($event) { 72 * // the data can be accessed via $event->data 73 * }, $data); 74 * ~~~ 75 * 76 * A behavior is an instance of [[Behavior]] or its child class. A component can be attached with one or multiple 77 * behaviors. When a behavior is attached to a component, its public properties and methods can be accessed via the 78 * component directly, as if the component owns those properties and methods. 79 * 行为是行为的一个实例或它的子类。一个组件可以连接一个或多个 80 * 行为。当一个行为被连接到一个组件,它的公共属性和方法可以通过访问 81 * 组件的直接组件,因为组件拥有这些属性和方法。 82 * To attach a behavior to a component, declare it in [[behaviors()]], or explicitly call [[attachBehavior]]. Behaviors 83 * declared in [[behaviors()]] are automatically attached to the corresponding component. 84 * 85 * One can also attach a behavior to a component when configuring it with a configuration array. The syntax is like the 86 * following: 87 * 88 * ~~~ 89 * [ 90 * 'as tree' => [ 91 * 'class' => 'Tree', 92 * ], 93 * ] 94 * ~~~ 95 * 96 * where `as tree` stands for attaching a behavior named `tree`, and the array will be passed to [[\Yii::createObject()]] 97 * to create the behavior object. 98 * 99 * @property Behavior[] $behaviors List of behaviors attached to this component. This property is read-only. 100 * 101 * @author Qiang Xue <qiang.xue@gmail.com> 102 * @since 2.0 103 */ 104 //该类继承了Object类 105 class Component extends Object 106 { 107 /** 108 * @var array the attached event handlers (event name => handlers) 109 * 存储对象的事件处理程序,是一个数组 110 */ 111 private $_events = []; 112 /** 113 * @var Behavior[]|null the attached behaviors (behavior name => behavior). This is `null` when not initialized. 114 * 赋值给对象的行为,默认值为null 115 */ 116 private $_behaviors; 117 118 119 /** 120 * Returns the value of a component property. 121 * 得到属性值 122 * This method will check in the following order and act accordingly: 123 * 124 * - a property defined by a getter: return the getter result 125 * - a property of a behavior: return the behavior property value 126 * 127 * Do not call this method directly as it is a PHP magic method that 128 * 重写 Object 中的 getter 方法 129 * 返回一个组件的属性值。 130 * 这个方法将检查以下顺序并采取相应的行动: 131 * 132 * - 通过一个getter定义的属性:返回getter的结果 133 * - 一个行为的属性:返回的行为属性值 134 * 135 * 不要直接调用此方法,因为它是一个PHP魔术方法 136 * will be implicitly called when executing `$value = $component->property;`. 137 * @param string $name the property name 参数属性名 138 * @return mixed the property value or the value of a behavior's property 返回混合属性的值或行为属性的值 139 * @throws UnknownPropertyException if the property is not defined 如果没有定义,抛出信息 140 * @throws InvalidCallException if the property is write-only. 该属性是只写的 141 * @see __set() 142 */ 143 public function __get($name) 144 { 145 $getter = 'get' . $name; //定义$getter 146 if (method_exists($this, $getter)) { 147 // read property, e.g. getName() 148 //方法存在且被调用时,读取属性 149 return $this->$getter(); 150 } else { 151 // behavior property 152 $this->ensureBehaviors(); //确保行为已经绑定 153 foreach ($this->_behaviors as $behavior) { 154 if ($behavior->canGetProperty($name)) { 155 /** 156 * 判断检查对象或类是否能够获取 $name 属性, 157 * 如果 behavior 中含有该属性,就返回 behavior 中的这个属性 158 * 第二个参数为 true(默认是true),则不局限于是否有 getter 159 */ 160 return $behavior->$name; 161 } 162 } 163 } 164 if (method_exists($this, 'set' . $name)) {//方法存在且调用,返回true 165 throw new InvalidCallException('Getting write-only property: ' . get_class($this) . '::' . $name); 166 //抛出只读属性 167 } else { 168 throw new UnknownPropertyException('Getting unknown property: ' . get_class($this) . '::' . $name); 169 //错误扔出异常 170 } 171 } 172 173 /** 174 * Sets the value of a component property. 设置属性值 175 * This method will check in the following order and act accordingly:检查并执行 176 * 177 * - a property defined by a setter: set the property value 178 * - an event in the format of "on xyz": attach the handler to the event "xyz" 179 * - a behavior in the format of "as xyz": attach the behavior named as "xyz" 180 * - a property of a behavior: set the behavior property value 181 * 重写 Object 中的 setter 方法 182 * 由一个setter定义的属性:设置属性值。 183 * 如果 $name 是 'on xyz',就会将 xyz 事件添加到该对象中 184 * 如果 $name 是 'as xyz',就会将 xyz 行为添加到该对象中 185 * 添加对 behaviors 的处理,循环 behaviors,如果其中有相应的属性,就设置它 186 * Do not call this method directly as it is a PHP magic method that 187 * will be implicitly called when executing `$component->property = $value;`. 188 * @param string $name the property name or the event name 属性名或者事件名 189 * @param mixed $value the property value 属性值 190 * @throws UnknownPropertyException if the property is not defined 未定义扔出错误 191 * @throws InvalidCallException if the property is read-only. 抛出信息 只读 192 * @see __get() 193 */ 194 public function __set($name, $value) 195 { 196 $setter = 'set' . $name;//在属性名前面加set构建setter方法 197 if (method_exists($this, $setter)) { 198 // set property 199 //如果setter方法存在返回true,没有返回false. 调用该方法设置属性值 200 $this->$setter($value); 201 202 return; 203 } elseif (strncmp($name, 'on ', 3) === 0) {//比较$name和'on '前3个字符,如果$name以on+空格开始,就添加事件 204 // on event: attach event handler 205 $this->on(trim(substr($name, 3)), $value);//执行on 方法,用来添加附加事件。 206 207 return; 208 } elseif (strncmp($name, 'as ', 3) === 0) {//如果$name以as+空格开始,就添加行为 209 // as behavior: attach behavior 210 $name = trim(substr($name, 3));//截取as后面的字符,用attachBehavior来添加附加行为。 211 //$value这个对象是Behavior类的一个实例,取$value为参数,否则静态调用Yii方法创造一个新的对象。 212 $this->attachBehavior($name, $value instanceof Behavior ? $value : Yii::createObject($value)); 213 214 return; 215 } else { 216 // behavior property 217 $this->ensureBehaviors();//确保行为已经绑定 218 foreach ($this->_behaviors as $behavior) { 219 if ($behavior->canSetProperty($name)) {//遍历行为,如果行为中有可以设置的属性$name 220 $behavior->$name = $value;//给该行为类中的属性设置属性值 221 222 return; 223 } 224 } 225 } 226 if (method_exists($this, 'get' . $name)) {//如果'get'.$name这个方法存在,就抛出只读信息. 227 throw new InvalidCallException('Setting read-only property: ' . get_class($this) . '::' . $name); 228 } else { 229 //如果不属于这个类,就抛出异常. 230 throw new UnknownPropertyException('Setting unknown property: ' . get_class($this) . '::' . $name); 231 } 232 } 233 234 /** 235 * Checks if a property value is null. 236 * 属性是否为空 237 * This method will check in the following order and act accordingly: 238 * 239 * - a property defined by a setter: return whether the property value is null 240 * - a property of a behavior: return whether the property value is null 241 * 重写 Object 中的 isset 方法 通过一个setter定义的属性:返回属性是否设置 242 * 行为的属性:返回属性值是否设置 243 * Do not call this method directly as it is a PHP magic method that 244 * will be implicitly called when executing `isset($component->property)`. 245 * @param string $name the property name or the event name 属性名或事件名 246 * @return boolean whether the named property is null 属性值是否被设置 返回true/boolean值。 247 */ 248 public function __isset($name) 249 { 250 $getter = 'get' . $name; 251 if (method_exists($this, $getter)) { 252 // 判断是否有getter方法,有 getter 方法且获取的值不为 null,才认为该属性存在 253 return $this->$getter() !== null; 254 } else { 255 // behavior property 256 $this->ensureBehaviors();//确定行为已经绑定 257 foreach ($this->_behaviors as $behavior) {//遍历行为 258 if ($behavior->canGetProperty($name)) { 259 // 如果 behavior 中有 $name 属性,且不为 null,该属性存在 返回 true 260 return $behavior->$name !== null; 261 } 262 } 263 } 264 return false; 265 } 266 267 /** 268 * Sets a component property to be null. 269 * 设置属性为null,即删除属性 270 * This method will check in the following order and act accordingly: 271 * 272 * - a property defined by a setter: set the property value to be null 273 * - a property of a behavior: set the property value to be null 274 * 重写 Object 中的 unset 方法, 275 * 通过setter定义的属性:设置该属性值为空 276 * 属性的行为:将属性值设为空 277 * Do not call this method directly as it is a PHP magic method that 278 * will be implicitly called when executing `unset($component->property)`. 279 * @param string $name the property name 属性名 280 * @throws InvalidCallException if the property is read only. 该属性只读,抛出异常 281 */ 282 public function __unset($name) 283 { 284 $setter = 'set' . $name; 285 if (method_exists($this, $setter)) { 286 //如果$setter方法存在,通过setter方法把它设置为空 287 $this->$setter(null); 288 return; 289 } else { 290 // behavior property 291 $this->ensureBehaviors(); 292 foreach ($this->_behaviors as $behavior) {//遍历行为 293 if ($behavior->canSetProperty($name)) {//存在$name值,设为空 294 $behavior->$name = null; 295 return; 296 } 297 } 298 } 299 //抛出异常 300 throw new InvalidCallException('Unsetting an unknown or read-only property: ' . get_class($this) . '::' . $name); 301 } 302 303 /** 304 * Calls the named method which is not a class method. 305 * 306 * This method will check if any attached behavior has 307 * the named method and will execute it if available. 308 * 调用方法名 重写 Object 中的 call 方法,添加对行为的处理,循环 behaviors, 309 * 如果其中有相应方法,就执行该 behavior 的方法 310 * Do not call this method directly as it is a PHP magic method that 311 * will be implicitly called when an unknown method is being invoked. 312 * @param string $name the method name 313 * @param array $params method parameters 314 * @return mixed the method return value 315 * @throws UnknownMethodException when calling unknown method 316 */ 317 public function __call($name, $params) 318 { 319 $this->ensureBehaviors(); 320 foreach ($this->_behaviors as $object) { 321 if ($object->hasMethod($name)) { 322 //行为中存在名为 $name 的方法,就执行它 323 //call_user_func_array 调用回调函数,并把一个数组参数作为回调函数的参数 324 return call_user_func_array([$object, $name], $params); 325 } 326 } 327 //抛出异常 328 throw new UnknownMethodException('Calling unknown method: ' . get_class($this) . "::$name()"); 329 } 330 331 /** 332 * This method is called after the object is created by cloning an existing one. 333 * It removes all behaviors because they are attached to the old object. 334 * 通过克隆现有创建的对象后,此方法会被调用。 335 * 他将会消除所有的行为。所有的引用属性 仍然会是一个指向原来的变量的引用 336 */ 337 public function __clone() 338 { 339 // 对象复制时,将它的 _events 设置为空数组,将 _behaviors 设置为 null 340 $this->_events = []; 341 $this->_behaviors = null; 342 }
未完待续