Next we continue to understand component.php
The directory is: vendor/yiisoft/yii2/base/component.php
(Last code is received)
/** * Sets the value of a component property.
* Set the value of a component property. * This method would check in the following order and act accordingly: * This approach will examine the following order and take action accordingly: *-a property defined By a Setter:set the property value
Property defined by a setter: Sets the property value. *-an event in the format of ' on XYZ ': Attach the handler to the event "XYZ"
An "on XYZ" format event that the handler attaches to the event "XYZ". *-a behavior in the format of "as XYZ": Attach the behavior named as "XYZ"
The behavior of an "as XYZ" format, which is appended to the behavior named "XYZ". *-a property of a Behavior:set the behavior properties value * A behavior attribute: Sets the property value of the behavior * does not call this method directl Y as it is a PHP magic method that * would be implicitly called when executing ' $component->property = $value; '.
Do not call this method directly, because it is a PHP magic method that is called when executing $component->proerty = $value.
* @param string $name The property name or the event name
Attribute name or event name is character * @param mixed $value the property value
$value for mixed type * @throws Unknownpropertyexception if the property was not defined
If no attribute is defined, an exception message is thrown. * @throws Invalidcallexception If the property is Read-only.
If the property is read-only, an exception message is thrown. * @see __get ()*/ Publicfunction __set ($name, $value) {$setter='Set'. $name; if(Method_exists ($ This, $setter)) {//If $setter This method is called in this class, returns true and does not return false. //Set Property$ This-$setter ($value); Call the $setter method. return; } elseif (strncmp ($name,' on',3) ===0{//Compare $name and ' on ' first 3 characters, if the first three characters are equal, that is, when $name is ' on ' format,//On Event:attach event handler$ This->on (Trim (substr ($name,3) ), $value);//Executes the on method, which is used to add additional events. return; } elseif (strncmp ($name,' as',3) ===0{//compare $name and ' as ' first 3 characters, if the first three characters are equal, that is, when $name is ' as ' format,//As Behavior:attach behavior$name = Trim (substr ($name,3)); $ This->attachbehavior ($name, $value instanceof Behavior?$value: Yii::createobject ($value)); Additional behavior. $name and $value are arguments, followed by ternary operations, if $value This object is an instance of the behavior class, take $value as a parameter, otherwise Statically invokes the Yii method to create a new object. return; } Else { //Behavior Property$ This-ensurebehaviors ();//Attach a new event. foreach($ This->_behaviors as$behavior) { if($behaviorCansetproperty ($name)) {//If the parameter returns True, $behavior-$name = $value, define the property. $behavior$name =$value; return; } } } if(Method_exists ($ This,'Get'. $name)) {//if ' get '. $name This method belongs to this class, a message "set read-only property" is thrown. Throw NewInvalidcallexception ('Setting read-only Property:'. Get_class ($ This) .'::'. $name); } Else{//If it does not belong to this class, it throws a message "Set unknown property".
Throw NewUnknownpropertyexception ('Setting Unknown Property:'. Get_class ($ This) .'::'. $name); } } /** * Checks If a property was set, i.e. defined and NOT NULL. The
checks the property settings, which is defined, not NULL.
* This method would check in the following order and act accordingly: * This approach will check the following order and take the appropriate action:
*-a PR Operty defined by a Setter:return whether the property is set
properties defined by a setter: Returns whether the attribute is set
*-a property of a Behavior:return whether the property is set
properties of a behavior: Returns whether the property sets *-return ' false ' for non existing properties * Returns false for non-existing properties. * Do not call the This method directly as it's a PHP magic method that * would be implicitly called when executing ' isset ( $component->property) '.
Do not call this method directly, because it is a PHP magic method that will be called when the Isset ($component->property) is executed.
* @param string $name The property name or the event name * @return A Boolean whether the named property is Whether the set
property is set to return a Boolean value. * @see http://php.net/manual/en/function.isset.php */ Publicfunction __isset ($name) {$getter='Get'. $name; if(Method_exists ($ This, $getter)) {//If the $getter method does not exist in this class, return this method so that he is not null. return$ This$getter ()!==NULL; } Else { //Behavior Property$ This-ensurebehaviors (); Otherwise, attach a time. foreach($ This->_behaviors as$behavior) {//Traversal behavior, if this method returns to True, returns this property so that he is not null. if($behaviorCangetproperty ($name)) { return$behavior-$name!==NULL; } } } return false; } /** * Sets a component property to IS null.
Sets the property of a component to null.
* This method would check in the following order and act accordingly: * This approach will examine the following order and take action accordingly: *-a property defined By a Setter:set the property value is null
Property defined by a setter: property value is set to NULL
*-a property of a Behavior:set the attribute value to IS null * one behavior of the properties: property value set to NULL
* Do not call the This method directly as it's a PHP magic method that * would be implicitly called when executing ' unset ( $component->property) '.
Do not call this method directly, as it is a PHP magic method that will be called when executing ' unset ($component->property) '.
* @param string $name The property name * @throws Invalidcallexception If the properties is read only.//If this attribute is read-only then a letter is thrown Interest. * @seehttp://php.net/manual/en/function.unset.php */ Publicfunction __unset ($name) {$setter='Set'. $name; if(Method_exists ($ This, $setter)) {//If the $setter method exists in this class, execute the $this-> $setter (null) method. $ This$setter (NULL); return; } Else { //Behavior Property$ This-ensurebehaviors ();//Additional Eventsforeach($ This->_behaviors as$behavior) { if($behaviorCansetproperty ($name)) {//Traversal behavior event, if true, make $behavior-> $name = null. return. $behavior$name =NULL; return; } } } Throw NewInvalidcallexception ('unsetting an unknown or read-only property:'. Get_class ($ This) .'::'. $name);//Otherwise throws the message "reset an unknown or read-only property". } /** * Calls the named method which is not a class method. * Call Method Name, not a method of a class. * This method would check if any attached behavior have * The named method and would execute it if available.
This method checks whether there is an additional behavior that has
The naming method (if available) executes it.
* Don't call this method directly as it's a PHP magic method that * would be implicitly called when an unknown method is being invoked.
Do not call this method directly, because he is a PHP magic method, when an unknown method executes when this magic method will be called.
* @param string $name The method name * @param array $params method parameters * @return Mixed the method return value * @throws unknownmethodexception when calling unknown method*/ Publicfunction __call ($name, $params) { $ This-ensurebehaviors ();//Additional Eventsforeach($ This->_behaviors as$Object) { if($Object-Hasmethod ($name)) { returnCall_user_func_array ([$Object, $name], $params);//Use an array as a parameter to invoke a callback function, the return value is the result of the callback function execution, or FALSE. } } Throw NewUnknownmethodexception ('calling unknown method:'. Get_class ($ This) .":: $name ()"); Throws a message that calls an unknown method. } /** * This method was called after the object was created by cloning a existing one. * It removes all behaviors because they is attached to the old object. */
This method is called when an existing created object is cloned.
public function __clone () {$this ->_events = [];//event is an empty array. $ this ->_behaviors = null Span style= "color: #000000;" >;/ /behavior is null. /* * * Returns a value indicating whethe R A property was defined for this component.
* Returns a value indicating whether the property is defined for this component. * A property is defined if:
* A defined attribute if: *
*-The class has a getter or setter method associated with the specified name
* The class has a getter or setter method that is associated with the specified name * (in the case, the property name is case-insensitive);
* (In this case, the attribute name is not case sensitive); *-The class has a member variable with the specified name (when ' $checkVars ' is true);
* The class has a member variable with the specified name (when ' $ Checkvars ' is true) *-an attached behavior have a property of the the given name (when ' $checkBehaviors ' is true). * An additional behavior with the given name of the property (when ' $checkBehaviors ' is true). * @param string $name The property name * @param A Boolean $checkVars whether to treat member variables as properties * @param boolean $checkBehaviors whether to treat behaviors ' properties as properties of this component * @return Bo Olean whether the property is defined * @see cangetproperty () * @see Cansetproperty ()*/ Publicfunction Hasproperty ($name, $checkVars =true, $checkBehaviors =true) { return$ This->cangetproperty ($name, $checkVars, $checkBehaviors) | | $ This->cansetproperty ($name,false, $checkBehaviors);
YII2 Framework Essay 4