Component类
来源:互联网 发布:mysql高级教程 百度云 编辑:程序博客网 时间:2024/06/03 20:05
原文地址: http://www.cnblogs.com/dragon16/p/5521199.html
组件(component),是Yii框架的基类,实现了属性、事件、行为三类功能,如果需要事件和行为的功能,需要继承该类。
<?php/** * @link http://www.yiiframework.com/ * @copyright Copyright (c) 2008 Yii Software LLC * @license http://www.yiiframework.com/license/ */namespace yii\base;//命名空间use Yii;//加载Yii相关类/** * Component is the base class that implements the *property*, *event* and *behavior* features. * 组件是实现属性、事件、行为的基类 * Component provides the *event* and *behavior* features, in addition to the *property* feature which is implemented in * its parent class [[Object]]. * 组件提供事件和行为,继承他父类Object的特点 * Event is a way to "inject" custom code into existing code at certain places. For example, a comment object can trigger * 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 * is triggered (i.e. comment will be added), our custom code will be executed. * * An event is identified by a name that should be unique within the class it is defined at. Event names are *case-sensitive*. * 事件是一种在特定的地方“注入”自定义代码到现有的代码。例如, * 当一个用户添加评论事件时,注释的对象可以触发。我们可以编写自定义代码,并将其附加到此事件,这样当事件被触发(即评论将被添加), * * 我们的自定义代码会被执行, * One or multiple PHP callbacks, called *event handlers*, can be attached to an event. You can call [[trigger()]] to * raise an event. When an event is raised, the event handlers will be invoked automatically in the order they were * attached. * * To attach an event handler to an event, call [[on()]]: * 一个或多个php回调,称为为‘事件处理’。要触发一个事件处理,可以用on();举个例子。 * ~~~ * $post->on('update', function ($event) { * // send email notification * }); * ~~~ * * In the above, an anonymous function is attached to the "update" event of the post. You may attach * the following types of event handlers: * 一个匿名函数被连接到'update'事件后。可以用以下类型的程序做处理: * - anonymous function: `function ($event) { ... }` 匿名函数 * - object method: `[$object, 'handleAdd']` 对象方法 * - static class method: `['Page', 'handleAdd']` 静态类方法 * - global function: `'handleAdd'` 全局函数 * * The signature of an event handler should be like the following: * 一个事件处理程序的签名应该是如下: * ~~~ * function foo($event) * ~~~ * * where `$event` is an [[Event]] object which includes parameters associated with the event. * $event是一个[[Event]]对象 包括与事件相关联的参数。 * You can also attach a handler to an event when configuring a component with a configuration array. * The syntax is like the following: * 还可以将处理程序附加到配置数组中配置组件的事件。语法如下: * ~~~ * [ * 'on add' => function ($event) { ... } * ] * ~~~ * * where `on add` stands for attaching an event to the `add` event. * `on add` 代表添加事件 * Sometimes, you may want to associate extra data with an event handler when you attach it to an event * and then access it when the handler is invoked. You may do so by * 有时,当你将它附加到一个事件时,你可能会把额外的数据与事件处理程序关联起来 * 然后在调用处理程序时访问它。 * ~~~ * $post->on('update', function ($event) { * // the data can be accessed via $event->data * }, $data); * ~~~ * * A behavior is an instance of [[Behavior]] or its child class. A component can be attached with one or multiple * behaviors. When a behavior is attached to a component, its public properties and methods can be accessed via the * component directly, as if the component owns those properties and methods. * 行为是行为的一个实例或它的子类。一个组件可以连接一个或多个 * 行为。当一个行为被连接到一个组件,它的公共属性和方法可以通过访问 * 组件的直接组件,因为组件拥有这些属性和方法。 * To attach a behavior to a component, declare it in [[behaviors()]], or explicitly call [[attachBehavior]]. Behaviors * declared in [[behaviors()]] are automatically attached to the corresponding component. * * One can also attach a behavior to a component when configuring it with a configuration array. The syntax is like the * following: * * ~~~ * [ * 'as tree' => [ * 'class' => 'Tree', * ], * ] * ~~~ * * where `as tree` stands for attaching a behavior named `tree`, and the array will be passed to [[\Yii::createObject()]] * to create the behavior object. * * @property Behavior[] $behaviors List of behaviors attached to this component. This property is read-only. * * @author Qiang Xue <qiang.xue@gmail.com> * @since 2.0 *///该类继承了Object类class Component extends Object{ /** * @var array the attached event handlers (event name => handlers) * 存储对象的事件处理程序,是一个数组 */ private $_events = []; /** * @var Behavior[]|null the attached behaviors (behavior name => behavior). This is `null` when not initialized. * 赋值给对象的行为,默认值为null */ private $_behaviors; /** * Returns the value of a component property. * 得到属性值 * This method will check in the following order and act accordingly: * * - a property defined by a getter: return the getter result * - a property of a behavior: return the behavior property value * * Do not call this method directly as it is a PHP magic method that * 重写 Object 中的 getter 方法 * 返回一个组件的属性值。 * 这个方法将检查以下顺序并采取相应的行动: * * - 通过一个getter定义的属性:返回getter的结果 * - 一个行为的属性:返回的行为属性值 * * 不要直接调用此方法,因为它是一个PHP魔术方法 * will be implicitly called when executing `$value = $component->property;`. * @param string $name the property name 参数属性名 * @return mixed the property value or the value of a behavior's property 返回混合属性的值或行为属性的值 * @throws UnknownPropertyException if the property is not defined 如果没有定义,抛出信息 * @throws InvalidCallException if the property is write-only. 该属性是只写的 * @see __set() */ public function __get($name) { $getter = 'get' . $name; //定义$getter if (method_exists($this, $getter)) { // read property, e.g. getName() //方法存在且被调用时,读取属性 return $this->$getter(); } else { // behavior property $this->ensureBehaviors(); //确保行为已经绑定 foreach ($this->_behaviors as $behavior) { if ($behavior->canGetProperty($name)) { /** * 判断检查对象或类是否能够获取 $name 属性, * 如果 behavior 中含有该属性,就返回 behavior 中的这个属性 * 第二个参数为 true(默认是true),则不局限于是否有 getter */ return $behavior->$name; } } } if (method_exists($this, 'set' . $name)) {//方法存在且调用,返回true throw new InvalidCallException('Getting write-only property: ' . get_class($this) . '::' . $name); //抛出只读属性 } else { throw new UnknownPropertyException('Getting unknown property: ' . get_class($this) . '::' . $name); //错误扔出异常 } } /** * Sets the value of a component property. 设置属性值 * This method will check in the following order and act accordingly:检查并执行 * * - a property defined by a setter: set the property value * - an event in the format of "on xyz": attach the handler to the event "xyz" * - a behavior in the format of "as xyz": attach the behavior named as "xyz" * - a property of a behavior: set the behavior property value * 重写 Object 中的 setter 方法 * 由一个setter定义的属性:设置属性值。 * 如果 $name 是 'on xyz',就会将 xyz 事件添加到该对象中 * 如果 $name 是 'as xyz',就会将 xyz 行为添加到该对象中 * 添加对 behaviors 的处理,循环 behaviors,如果其中有相应的属性,就设置它 * Do not call this method directly as it is a PHP magic method that * will be implicitly called when executing `$component->property = $value;`. * @param string $name the property name or the event name 属性名或者事件名 * @param mixed $value the property value 属性值 * @throws UnknownPropertyException if the property is not defined 未定义扔出错误 * @throws InvalidCallException if the property is read-only. 抛出信息 只读 * @see __get() */ public function __set($name, $value) { $setter = 'set' . $name;//在属性名前面加set构建setter方法 if (method_exists($this, $setter)) { // set property //如果setter方法存在返回true,没有返回false. 调用该方法设置属性值 $this->$setter($value); return; } elseif (strncmp($name, 'on ', 3) === 0) {//比较$name和'on '前3个字符,如果$name以on+空格开始,就添加事件 // on event: attach event handler $this->on(trim(substr($name, 3)), $value);//执行on 方法,用来添加附加事件。 return; } elseif (strncmp($name, 'as ', 3) === 0) {//如果$name以as+空格开始,就添加行为 // as behavior: attach behavior $name = trim(substr($name, 3));//截取as后面的字符,用attachBehavior来添加附加行为。 //$value这个对象是Behavior类的一个实例,取$value为参数,否则静态调用Yii方法创造一个新的对象。 $this->attachBehavior($name, $value instanceof Behavior ? $value : Yii::createObject($value)); return; } else { // behavior property $this->ensureBehaviors();//确保行为已经绑定 foreach ($this->_behaviors as $behavior) { if ($behavior->canSetProperty($name)) {//遍历行为,如果行为中有可以设置的属性$name $behavior->$name = $value;//给该行为类中的属性设置属性值 return; } } } if (method_exists($this, 'get' . $name)) {//如果'get'.$name这个方法存在,就抛出只读信息. throw new InvalidCallException('Setting read-only property: ' . get_class($this) . '::' . $name); } else { //如果不属于这个类,就抛出异常. throw new UnknownPropertyException('Setting unknown property: ' . get_class($this) . '::' . $name); } } /** * Checks if a property value is null. * 属性是否为空 * This method will check in the following order and act accordingly: * * - a property defined by a setter: return whether the property value is null * - a property of a behavior: return whether the property value is null * 重写 Object 中的 isset 方法 通过一个setter定义的属性:返回属性是否设置 * 行为的属性:返回属性值是否设置 * Do not call this method directly as it is a PHP magic method that * will be implicitly called when executing `isset($component->property)`. * @param string $name the property name or the event name 属性名或事件名 * @return boolean whether the named property is null 属性值是否被设置 返回true/boolean值。 */ public function __isset($name) { $getter = 'get' . $name; if (method_exists($this, $getter)) { // 判断是否有getter方法,有 getter 方法且获取的值不为 null,才认为该属性存在 return $this->$getter() !== null; } else { // behavior property $this->ensureBehaviors();//确定行为已经绑定 foreach ($this->_behaviors as $behavior) {//遍历行为 if ($behavior->canGetProperty($name)) { // 如果 behavior 中有 $name 属性,且不为 null,该属性存在 返回 true return $behavior->$name !== null; } } } return false; } /** * Sets a component property to be null. * 设置属性为null,即删除属性 * This method will check in the following order and act accordingly: * * - a property defined by a setter: set the property value to be null * - a property of a behavior: set the property value to be null * 重写 Object 中的 unset 方法, * 通过setter定义的属性:设置该属性值为空 * 属性的行为:将属性值设为空 * Do not call this method directly as it is a PHP magic method that * will be implicitly called when executing `unset($component->property)`. * @param string $name the property name 属性名 * @throws InvalidCallException if the property is read only. 该属性只读,抛出异常 */ public function __unset($name) { $setter = 'set' . $name; if (method_exists($this, $setter)) { //如果$setter方法存在,通过setter方法把它设置为空 $this->$setter(null); return; } else { // behavior property $this->ensureBehaviors(); foreach ($this->_behaviors as $behavior) {//遍历行为 if ($behavior->canSetProperty($name)) {//存在$name值,设为空 $behavior->$name = null; return; } } } //抛出异常 throw new InvalidCallException('Unsetting an unknown or read-only property: ' . get_class($this) . '::' . $name); } /** * Calls the named method which is not a class method. * * This method will check if any attached behavior has * the named method and will execute it if available. * 调用方法名 重写 Object 中的 call 方法,添加对行为的处理,循环 behaviors, * 如果其中有相应方法,就执行该 behavior 的方法 * Do not call this method directly as it is a PHP magic method that * will be implicitly called when an unknown method is being invoked. * @param string $name the method name * @param array $params method parameters * @return mixed the method return value * @throws UnknownMethodException when calling unknown method */ public function __call($name, $params) { $this->ensureBehaviors(); foreach ($this->_behaviors as $object) { if ($object->hasMethod($name)) { //行为中存在名为 $name 的方法,就执行它 //call_user_func_array 调用回调函数,并把一个数组参数作为回调函数的参数 return call_user_func_array([$object, $name], $params); } } //抛出异常 throw new UnknownMethodException('Calling unknown method: ' . get_class($this) . "::$name()"); } /** * This method is called after the object is created by cloning an existing one. * It removes all behaviors because they are attached to the old object. * 通过克隆现有创建的对象后,此方法会被调用。 * 他将会消除所有的行为。所有的引用属性 仍然会是一个指向原来的变量的引用 */ public function __clone() { // 对象复制时,将它的 _events 设置为空数组,将 _behaviors 设置为 null $this->_events = []; $this->_behaviors = null; }
/** * Returns a value indicating whether a property is defined for this component. * A property is defined if: * * - the class has a getter or setter method associated with the specified name * (in this case, property name is case-insensitive); * - the class has a member variable with the specified name (when `$checkVars` is true); * - an attached behavior has a property of the given name (when `$checkBehaviors` is true). * 与 Object 中的方法类似,只是添加了是否检测行为的参数 * @param string $name the property name 属性名 * @param boolean $checkVars whether to treat member variables as properties * @param boolean $checkBehaviors whether to treat behaviors' properties as properties of this component * 是否将行为属性作为该组件的属性来对待 * @return boolean whether the property is defined 属性是否定义 * @see canGetProperty() 可读 * @see canSetProperty() 可设置 */ public function hasProperty($name, $checkVars = true, $checkBehaviors = true) { // $checkVars 参数,用来检查对象是否具有该属性 (不是 getter 和 setter 定义出的属性) //$checkBehaviors参数,用来设置是否检测behavior return $this->canGetProperty($name, $checkVars, $checkBehaviors) || $this->canSetProperty($name, false, $checkBehaviors); } /** * Returns a value indicating whether a property can be read. * A property can be read if: * * - the class has a getter method associated with the specified name * (in this case, property name is case-insensitive); * - the class has a member variable with the specified name (when `$checkVars` is true); * - an attached behavior has a readable property of the given name (when `$checkBehaviors` is true). * 检查对象或类是否能够获取 $name 属性 * @param string $name the property name 属性名 * @param boolean $checkVars whether to treat member variables as properties 是否将成员对象作为属性 * @param boolean $checkBehaviors whether to treat behaviors' properties as properties of this component * 是否将成员属性作为该组件的属性 * @return boolean whether the property can be read 属性是否可读 * @see canSetProperty() */ public function canGetProperty($name, $checkVars = true, $checkBehaviors = true) { if (method_exists($this, 'get' . $name) || $checkVars && property_exists($this, $name)) { // 存在 'get' . $name方法 或 存在该属性, 返回true return true; } elseif ($checkBehaviors) { $this->ensureBehaviors(); foreach ($this->_behaviors as $behavior) { // behavior 中存在名为 $name 的可读属性,认为该对象也存在 返回true if ($behavior->canGetProperty($name, $checkVars)) { return true; } } } return false; } /** * Returns a value indicating whether a property can be set. * A property can be written if: * * - the class has a setter method associated with the specified name * (in this case, property name is case-insensitive); * - the class has a member variable with the specified name (when `$checkVars` is true); * - an attached behavior has a writable property of the given name (when `$checkBehaviors` is true). * 检查对象或类是否能够设置 $name 属性 * @param string $name the property name 属性名 * @param boolean $checkVars whether to treat member variables as properties 是否将成员变量作为属性 * @param boolean $checkBehaviors whether to treat behaviors' properties as properties of this component * 是否将行为属性作为该组件的属性 * @return boolean whether the property can be written 属性是否可写 * @see canGetProperty() */ public function canSetProperty($name, $checkVars = true, $checkBehaviors = true) { if (method_exists($this, 'set' . $name) || $checkVars && property_exists($this, $name)) { // 存在 'set' . $name方法 或 存在该属性, 返回true return true; } elseif ($checkBehaviors) { $this->ensureBehaviors(); foreach ($this->_behaviors as $behavior) { // behavior 中存在名为 $name 的可写属性,认为该对象也存在 返回true if ($behavior->canSetProperty($name, $checkVars)) { return true; } } } return false; } /** * Returns a value indicating whether a method is defined. * A method is defined if: * * - the class has a method with the specified name * - an attached behavior has a method with the given name (when `$checkBehaviors` is true). * 检查对象或类是否具有 $name 方法 * @param string $name the property name * @param boolean $checkBehaviors whether to treat behaviors' methods as methods of this component * @return boolean whether the property is defined */ public function hasMethod($name, $checkBehaviors = true) { if (method_exists($this, $name)) { return true; } elseif ($checkBehaviors) {//标记是否 check behavior 中的方法 $this->ensureBehaviors(); foreach ($this->_behaviors as $behavior) { // behavior 中存在名为 $name 的方法,就认为该方法也存在 if ($behavior->hasMethod($name)) { return true; } } } return false; } /** * Returns a list of behaviors that this component should behave as. * 定义该对象中要用到的 behavior * Child classes may override this method to specify the behaviors they want to behave as. * 可重写 * The return value of this method should be an array of behavior objects or configurations * indexed by behavior names. A behavior configuration can be either a string specifying * the behavior class or an array of the following structure: * * ~~~ * 'behaviorName' => [ * 'class' => 'BehaviorClass', * 'property1' => 'value1', * 'property2' => 'value2', * ] * ~~~ * * Note that a behavior class must extend from [[Behavior]]. Behavior names can be strings * or integers. If the former, they uniquely identify the behaviors. If the latter, the corresponding * behaviors are anonymous and their properties and methods will NOT be made available via the component * (however, the behaviors can still respond to the component's events). * 行为名称可以是字符串或整数。如果是前者,他们是唯一确定的行为。如果是后者,相应的 * 行为是匿名的,它们的特性和方法将无法通过组件提供(仍然可以对组件的事件作出反应) * Behaviors declared in this method will be attached to the component automatically (on demand). * 在这种方法中声明的行为将自动连接到组件 * @return array the behavior configurations. */ public function behaviors() { return []; } /** * Returns a value indicating whether there is any handler attached to the named event. * 判断 _events 中的一个事件是否具有事件处理程序 * @param string $name the event name 事件名 * @return boolean whether there is any handler attached to the event. */ public function hasEventHandlers($name) { $this->ensureBehaviors(); // 判断事件是否存在 $name 否则 调用Event类中的的方法判断是否有处理程序 return !empty($this->_events[$name]) || Event::hasHandlers($this, $name); } /** * Attaches an event handler to an event. * 处理程序的事件 * The event handler must be a valid PHP callback. The followings are * 事件处理程序必须是有效的PHP回调函数,方便内置方法call_user_fucn()调用 * some examples: * * ~~~ * function ($event) { ... } // anonymous function * [$object, 'handleClick'] // $object->handleClick() * ['Page', 'handleClick'] // Page::handleClick() * 'handleClick' // global function handleClick() * ~~~ * * The event handler must be defined with the following signature, * * ~~~ * function ($event) * ~~~ * * where `$event` is an [[Event]] object which includes parameters associated with the event. * * @param string $name the event name 事件名 * @param callable $handler the event handler 事件处理函数 * @param mixed $data the data to be passed to the event handler when the event is triggered. * 当事件被触发时,将传递给事件处理程序的数据。 * When the event handler is invoked, this data can be accessed via [[Event::data]]. * 当调用事件处理程序时,该数据可以通过 [[Event::data]] 访问 * @param boolean $append whether to append new event handler to the end of the existing * handler list. If false, the new handler will be inserted at the beginning of the existing * handler list. * 是否将新事件处理程序附加到现有的处理程序列表的结尾。如果是错的,新的处理器将被插入在列表的开始的处理程序列表。 * @see off() */ public function on($name, $handler, $data = null, $append = true) { $this->ensureBehaviors(); //$append 判断是否添加到事件(event)的后面,确保_events中有该事件 if ($append || empty($this->_events[$name])) { //将事件处理程序和参数添加到event数组末尾 $this->_events[$name][] = [$handler, $data]; } else { //否则 添加到 event 的前面 array_unshift($this->_events[$name], [$handler, $data]); } } /** * Detaches an existing event handler from this component. * This method is the opposite of [[on()]]. * [[on()]]的反方法,用于删除事件处理程序 * @param string $name event name 事件名 * @param callable $handler the event handler to be removed. 事件处理程序 * If it is null, all handlers attached to the named event will be removed. 如果为空,清除所有的事件处理程序 * @return boolean if a handler is found and detached 是否发现并分离的处理程序 * @see on() */ public function off($name, $handler = null) { $this->ensureBehaviors(); // 相应的事件不存在,返回false if (empty($this->_events[$name])) { return false; } //没有handler,清除该事件的所有事件处理程序 并返回true if ($handler === null) { unset($this->_events[$name]); return true; } else { $removed = false;//删除标记 foreach ($this->_events[$name] as $i => $event) { if ($event[0] === $handler) {//遍历该事件 判断事件处理程序是否符合 unset($this->_events[$name][$i]);//删除该事件处理程序 $removed = true; } } if ($removed) { // 如果删除成功,就需要重新构建以下索引,重新赋值 $this->_events[$name] = array_values($this->_events[$name]); } return $removed;//成功标记 } } /** * Triggers an event. 触发事件 * This method represents the happening of an event. It invokes * all attached handlers for the event including class-level handlers. * @param string $name the event name 事件名 * @param Event $event the event parameter. If not set, a default [[Event]] object will be created. * 事件参数. 如果未设置,默认的 [[Event]] 对象将被创建. */ public function trigger($name, Event $event = null) { //确保行为绑定 $this->ensureBehaviors(); if (!empty($this->_events[$name])) { // 事件名不为空 构建Event对象,为传入到handler函数中做准备 if ($event === null) { $event = new Event; } if ($event->sender === null) { $event->sender = $this; } $event->handled = false; $event->name = $name; foreach ($this->_events[$name] as $handler) { // 遍历事件 给事件的data属性赋值 $event->data = $handler[1]; // handler的函数中传入了一个Event对象 call_user_func($handler[0], $event); // stop further handling if the event is handled // 事件是否被处理,如果了处理事件即handled被设置为true时,停止进一步处理 if ($event->handled) { return; } } } // invoke class-level attached handlers [[Event]] 的方法,触发类级别的事件处理程序 Event::trigger($this, $name, $event); } /** * Returns the named behavior object.获取行为类 * @param string $name the behavior name 行为名 * @return Behavior the behavior object, or null if the behavior does not exist 行为对象,如果行为不存在为null */ public function getBehavior($name) { //确保行为绑定 $this->ensureBehaviors(); //_behaviors中的行为类存在,返回行为类名,否则返回空 return isset($this->_behaviors[$name]) ? $this->_behaviors[$name] : null; } /** * Returns all behaviors attached to this component. 获取所有的行为类 * @return Behavior[] list of behaviors attached to this component */ public function getBehaviors() { $this->ensureBehaviors(); //确保行为绑定 return $this->_behaviors; //直接返回所有行为 } /** * Attaches a behavior to this component. * 内部使用的添加一个行为到该组件 * This method will create the behavior object based on the given * configuration. After that, the behavior object will be attached to * this component by calling the [[Behavior::attach()]] method. * 通过提供的配置文件创建一个Behavior对象,通过调用 [[Behavior::attach()]] 方法添加行为到组件. * @param string $name the name of the behavior. 行为名 * @param string|array|Behavior $behavior the behavior configuration. This can be one of the following: * behavior配置 * - a [[Behavior]] object 一个[[Behavior]]类 * - a string specifying the behavior class 一个字符串形式的指定行为类 * - an object configuration array that will be passed to [[Yii::createObject()]] to create the behavior object. * 一个配置文件数组,通过调用[[Yii::createObject()]] 创建一个行为对象. * @return Behavior the behavior object 行为对象 * @see detachBehavior() */ public function attachBehavior($name, $behavior) { $this->ensureBehaviors();//确保行为绑定 return $this->attachBehaviorInternal($name, $behavior); //添加行为 } /** * Attaches a list of behaviors to the component. 添加行为列表到组件 * Each behavior is indexed by its name and should be a [[Behavior]] object, * a string specifying the behavior class, or an configuration array for creating the behavior. * 行为类通过行为名索引,且必须是一个 [[Behavior]] 对象指定的行为类或者一个配置数组 * @param array $behaviors list of behaviors to be attached to the component 行为列表 * @see attachBehavior() */ public function attachBehaviors($behaviors) { $this->ensureBehaviors();//确保行为绑定 foreach ($behaviors as $name => $behavior) { //添加行为列表 $this->attachBehaviorInternal($name, $behavior); } } /** * Detaches a behavior from the component. 从组件解除行为 * The behavior's [[Behavior::detach()]] method will be invoked. 通过[[Behavior::detach()]]解除行为 * @param string $name the behavior's name. 行为名 * @return Behavior the detached behavior. Null if the behavior does not exist. 存在返回分离行为 不存在返回null */ public function detachBehavior($name) { $this->ensureBehaviors();//确保行为绑定 if (isset($this->_behaviors[$name])) { //行为存在,解除行为 $behavior = $this->_behaviors[$name]; unset($this->_behaviors[$name]); //返回分离行为 $behavior->detach(); return $behavior; } else { return null; } } /** * Detaches all behaviors from the component. 解除所有行为 */ public function detachBehaviors() { $this->ensureBehaviors();//确保行为绑定 foreach ($this->_behaviors as $name => $behavior) { //遍历解除行为 $this->detachBehavior($name); } } /** * Makes sure that the behaviors declared in [[behaviors()]] are attached to this component. * 确保声明的行为都被添加到组件 */ public function ensureBehaviors() { if ($this->_behaviors === null) { $this->_behaviors = []; foreach ($this->behaviors() as $name => $behavior) { //遍历$this->behaviors()中的behaviors,并添加到$this->_behaviors数组中 $this->attachBehaviorInternal($name, $behavior); } } } /** * Attaches a behavior to this component. 内部使用的为该对象添加behavior的方法 * @param string|integer $name the name of the behavior. If this is an integer, it means the behavior * is an anonymous one. Otherwise, the behavior is a named one and any existing behavior with the same name * will be detached first. 行为名. 如果是整数,说明该行为是匿名的 * @param string|array|Behavior $behavior the behavior to be attached 添加的行为 string|array|Behavior * @return Behavior the attached behavior. */ private function attachBehaviorInternal($name, $behavior) { if (!($behavior instanceof Behavior)) { // $behavior不是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);//重新绑定行为到组件 $this->_behaviors[$name] = $behavior; } return $behavior; }
0 0
- Component类
- Ext笔记-> Component类
- UNITY 5.2 Component类
- @Component
- Component
- component
- @component
- 2.Component映射:工具类
- Yii2 Component基类分析
- openmax component类的继承关系
- openmax component类的继承关系
- Java Component类的常用方法
- 3. Component、Controller、Service、Repository注解类
- 实现一个类是component 和component-scan功能,实现mbatis spring 零配置
- <context:component-scan>扫描@Component @Controller@Service注解的的类 <mvc:annotation-driven />启用注解
- spring自动扫描java类(context:component-scan)
- spring启动component-scan类扫描加载过程---源码分析
- spring启动component-scan类扫描加载过程---源码分析
- Linux execl()函数练习
- GDOI第二轮模拟总结
- @SerializedName注解
- POJ 1797 (最短路变形)
- 软件测试基础-应急必备大全
- Component类
- 向量叉积——Luogu1355 神秘大三角
- 接口和多态
- 树莓派安装Python-OpenCV
- RDBMS与NOsql的区别
- main方法的格式讲解
- 网络编程
- dm.dll注册(大漠插件注册)
- 航海舰队