yii2框架随笔4
接下来我们继续了解Component.php
目录为:vendor/yiisoft/yii2/base/Component.php
(接上次的代码)
/** * 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
由一个setter定义的属性:设置属性值。 * - an event in the format of "on xyz": attach the handler to the event "xyz"
一个"on xyz"格式的事件,处理程序会附加到事件"xyz". * - a behavior in the format of "as xyz": attach the behavior named as "xyz"
一个"as xyz"格式的行为,此行为会附加到名字为"xyz"的行为后面。 * - a property of a behavior: set the behavior property value *一个行为的属性:设置行为的属性值 * Do not call this method directly as it is a PHP magic method that * will be implicitly called when executing `$component->property = $value;`.
不要直接调用此方法,因为它是一个PHP魔术方法,当执行 $component->proerty = $value 时被调用。
* @param string $name the property name or the event name
属性名称或事件名称为字符型 * @param mixed $value the property value
$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; if (method_exists($this, $setter)) { //如果$setter这个方法在此类中被调用,返回true,没有返回false. // set property $this->$setter($value); //调用$setter方法。 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 '前3个字符,如果前三个字符相等,即当$name是 'as '格式的时候, // as behavior: attach behavior $name = trim(substr($name, 3)); $this->attachBehavior($name, $value instanceof Behavior ? $value : Yii::createObject($value)); //附加行为。$name 和 $value为参数,后面为三元运算,如果$value这个对象是Behavior类的一个实例,取$value为参数,否则 静态调用Yii方法创造一个新的对象。 return; } else { // behavior property $this->ensureBehaviors(); //附加一个新的事件。 foreach ($this->_behaviors as $behavior) { if ($behavior->canSetProperty($name)) {//如果参数返回为真,$behavior->$name = $value,定义属性。 $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 is set, i.e. defined and not null.
检查属性设置,即定义,not null。
* This method will check in the following order and act accordingly: * 这种方法将检查以下顺序并采取相应的行动:
* - a property defined by a setter: return whether the property is set
通过一个setter定义的属性:返回属性是否设置
* - a property of a behavior: return whether the property is set
一个行为的属性:返回属性是否设置 * - return `false` for non existing properties * 对于非现有的属性返回false. * Do not call this method directly as it is a PHP magic method that * will be implicitly called when executing `isset($component->property)`.
不要直接调用此方法,因为它是一个PHP魔术方法,当执行isset($component->property)时此魔术方法会被调用。
* @param string $name the property name or the event name * @return boolean whether the named property is set
属性是否被设置返回boolean值。 * @see http://php.net/manual/en/function.isset.php */ public function __isset($name) { $getter = 'get' . $name; if (method_exists($this, $getter)) { //如果$getter方法不存在此类当中,返回这个方法,使他不为null. return $this->$getter() !== null; } else { // behavior property $this->ensureBehaviors(); //否则附加一个时间。 foreach ($this->_behaviors as $behavior) {//遍历行为,如果此方法返回为真,返回这个属性使他不为null. if ($behavior->canGetProperty($name)) { 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
通过一个setter定义的属性:属性值设置为空
* - a property of a behavior: set the property value to be null *一个行为的属性:属性值设置为空
* Do not call this method directly as it is a PHP magic method that * will be implicitly called when executing `unset($component->property)`.
不要直接调用此方法,因为它是一个PHP魔术方法,当执行'unset($component->property)'时此魔术方法会被调用。
* @param string $name the property name * @throws InvalidCallException if the property is read only.//如果这个属性为只读的话抛出一个信息。 * @see http://php.net/manual/en/function.unset.php */ public function __unset($name) { $setter = 'set' . $name; if (method_exists($this, $setter)) { //如果$setter方法存在这个类里面的话,执行$this->$setter(null)方法。 $this->$setter(null); return; } else { // behavior property $this->ensureBehaviors(); //附加事件 foreach ($this->_behaviors as $behavior) { if ($behavior->canSetProperty($name)) {//遍历行为事件,如果为真的话,使$behavior->$name = null.返回。 $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.
//此方法将检查是否有附加的行为有
命名方法(如果可用)将执行它。
* 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.
//不要直接调用此方法,因为他是一个PHP魔术方法,当一个未知方法执行时此魔术方法会被调用。
* @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)) { return call_user_func_array([$object, $name], $params);//用一个数组作为参数调用一个回调函数,返回值为回调函数执行的结果或者为false。 } } 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() { $this->_events = []; //事件为空数组。 $this->_behaviors = null;//行为为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
*该类具有与指定名称关联的getter或setter方法 * (in this case, property name is case-insensitive);
*(在这种情况下,属性名称不区分大小写); * - the class has a member variable with the specified name (when `$checkVars` is true);
*该类具有指定名称的成员变量(当`$ checkVars`为true) * - an attached behavior has a property of the given name (when `$checkBehaviors` is true). *一个附加行为具有给定的名称的属性(当`$checkBehaviors`为true)。 * @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) { return $this->canGetProperty($name, $checkVars, $checkBehaviors) || $this->canSetProperty($name, false, $checkBehaviors);