Yii2 Framework Essay 6

Source: Internet
Author: User

Now let us uncover the mysterious veil of YII2 's most basic class object.php class (he is the ancestor of all classes)!

Directory location is base/object.php

<?PHP/** * @linkhttp://www.yiiframework.com/* @copyright Copyright (c) Yii software LLC * @licensehttp://www.yiiframework.com/license/ */namespaceYii\Base; use Yii;/** * Object is the base class that implements the *property* feature. * Object is a basic class that implements the function of properties * * A property was defined by A getter method (e.g. ' Getlabel '), and/or a setter method (e.g. ' Set Label ').  For example, * the following getter and setter methods define a property named ' label ': * An attribute that defines the Getter method and/or Setter method * * ~ ~ * Private $_label; * Public Function Getlabel () * {* return $this->_label; *} * Public Function SetLabel ($value) * {* $this ->_label = $value; *} * ~ ~ * Property names is *case-insensitive*. * Attribute name is case sensitive * A property can is accessed like a member variable of an object. Reading or writing a property would cause the invocation * of the corresponding getter or setter method. For example, * properties can be used as member variables of an object * ~ ~ ~ ~//equivalent to $label = $object->getlabel (); * $label = $object->label; *//equivalent to $object->setlabel (' abc '); * $object->label = ' abc '; * ~ ~ * * If a property had only a getter method and have no setter meThod, it is considered as *read-only*. Trying * To modify the property value would cause an exception.  * One can call [[Hasproperty ()]], [[Cangetproperty ()]] and/or [[Cansetproperty ()]] to check the existence of a property. * * Besides the property feature, object also introduces a important object initialization life cycle. In particular, * creating a new instance of Object or its derived class would involve the following life cycles sequential LY: * * 1. The class constructor is invoked; * 2. Object properties is initialized according to the given configuration; * 3. The ' init () ' method is invoked. * In the above, both Step 2 and 3 occur at the end of the class constructor. IT is recommended this * perform object initialization in the ' init () ' method because on that stage, the object config Uration * is already applied.  * In order to ensure the above life cycles, if a child class of Object needs to override the constructor, * it should is Do like the FollowinG: * ~ ~ ~ * Public Function __construct ($param 1, $param 2, ..., $config = []) * {* ... * parent::__construct ($con FIG); *} * ~ ~ ~ * That's, a ' $config ' parameter (defaults to ' [] ') should was declared as the last parameter * of the CONSTRUC Tor, and the parent implementation should is called at the end of the constructor.  * * Yii most basic class, most classes inherit the class * * @author Qiang Xue <[email protected]> * @since 2.0*/classObject implements configurable{/** * Returns the fully qualified name of this class. * Gets the class name of the static method call.     Returns the name of the class, or FALSE if not called in the class.     * * @return String The fully qualified name of this class. */     Public Staticfunction ClassName () {//Get_called_class--the name of the late static binding ("late static binding") class//This is the method called by that class, which returns the class with the namespace in the return value.        returnGet_called_class (); }    /** * Constructor.      * The default implementation does and things: * *-initializes the object with the given configuration ' $config '.     *-Call [[Init ()]]. * Constructors, default implementations do two things
* Initializes the given configuration $config.
* Use the init () method. * If This method was overridden in a child class, it was recommended that * *-the last parameter of the Constructo R is a configuration array, like ' $config ' here. *-Call the parent implementation at the end of the constructor. * * @param array $config name-value pairs that'll be used to initialize the object properties*/ Publicfunction __construct ($config = []) { //initialize the object based on $config content if(!empty ($config)) {Yii::configure ($ This, $config); } //calling the Init () method, classes inheriting from the class can override the Init method for initializing$ This-init (); } /** * Initializes the object. * Initialize Object * This method was invoked at the end of the constructor after the object was initialized with the * given CO Nfiguration. */ Publicfunction init () {}/** * Returns The value of a object property. * Don't call the This method directly as it's a PHP magic method that * would be implicitly called when executing ' $value = $object->property; '. * * Magic method, Implement Getter * * @param string $name The property name * @return Mixed the property value * @thr oWS Unknownpropertyexception If the property isn't defined * @throws invalidcallexception if the property is Write-on LY * @see __set ()*/ Publicfunction __get ($name) {$getter='Get'. $name; if(Method_exists ($ This, $getter)) { //object exists $getter method, it is called directly return$ This-$getter (); } elseif (Method_exists ($ This,'Set'. $name)) { //If there is a ' set '. $name method, it is assumed that the property is write-only Throw NewInvalidcallexception ('Getting write-only Property:'. Get_class ($ This) .'::'. $name); } Else { //Otherwise, the attribute is not considered to exist Throw NewUnknownpropertyexception ('Getting Unknown Property:'. Get_class ($ This) .'::'. $name); } } /** * Sets value of a object property. * Don't call the This method directly as it's a PHP magic method that * would be implicitly called when executing ' $object->property = $value; '. * * Magic method, implement Setter * * @param string $name The property name or the event name * @param mixed $value the P Roperty value * @throws unknownpropertyexception if the property was not defined * @throws invalidcallexception if The property is Read-only * @see __get ()*/ Publicfunction __set ($name, $value) {$setter='Set'. $name; if(Method_exists ($ This, $setter)) { //object exists $setter method, it is called directly$ This-$setter ($value); } elseif (Method_exists ($ This,'Get'. $name)) { //If there is a ' get '. $name method, the property is considered read-only Throw NewInvalidcallexception ('Setting read-only Property:'. Get_class ($ This) .'::'. $name); } Else { //Otherwise, the attribute is not considered to exist Throw NewUnknownpropertyexception ('Setting Unknown Property:'. Get_class ($ This) .'::'. $name); } }
/** * Checks If the named property was set (NOT null). * Don't call the This method directly as it's a PHP magic method that * would be implicitly called when executing '     Isset ($object->property) '.     * * Note If the property is not defined, false would be returned.     * * Magic method, implement Isset, based on getter implementation, the property of getter method is counted to exist * * @param string $name The properties name or the event name     * @return Boolean whether the named property is set (NOT null). */     Publicfunction __isset ($name) {$getter='Get'. $name; if(Method_exists ($ This, $getter)) {            //There is a $getter method and the obtained value is not null before the attribute is considered to exist            return$ This$getter ()!==NULL; } Else {            return false; }    }
/** * Sets an object property to null. * Don't call the This method directly as it's a PHP magic method that * would be implicitly called when executing '     unset ($object->property) '.     * * Note If the property isn't defined, this method would do nothing.     * If The property is read-only, it'll throw an exception. * * Magic method, implement unset, based on setter implementation, with setter method properties to unset out * * @param string $name The property name * @throw     S Invalidcallexception if the property was read only. */     Publicfunction __unset ($name) {$setter='Set'. $name; if(Method_exists ($ This, $setter)) {            //set it to null by $setter method$ This$setter (NULL); } elseif (Method_exists ($ This,'Get'. $name)) {            //If there is a ' get '. $name method, the property is considered read-only            Throw NewInvalidcallexception ('unsetting read-only Property:'. Get_class ($ This) .'::'. $name); }    }

Yii2 Framework Essay 6

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.