This paper mainly introduces the properties of the Yii framework of PHP, and explains the steps of implementing the attributes in detail, and the friends can refer to them. We hope to help you.
In PHP, a member variable of a class is also called a property (properties). They are part of the class definition and are used to represent the state of an instance (that is, different instances of the zone classification). In practice, it is often possible to use a slightly more specific method to read and write attributes. For example, if there is a requirement to perform a trim operation on the Label property each time, you can do so in the following code:
$object->label = Trim ($label);
The disadvantage of the above code is that the trim () function must be called again whenever the Label property is modified. If you need to handle the Label property in the future, such as capitalization, you will have to modify all the code assigned to the Label property. This duplication of code can lead to bugs, and this practice obviously needs to be avoided as much as possible.
To solve this problem, Yii introduces a base class called Yii\base\object, which supports the definition of attributes based on getter and setter (readers and setters) methods within the class. If a class needs to support this feature, simply inherit the Yii\base\object or its subclasses.
Add: Almost every YII Framework's core class inherits from Yii\base\object or its subclasses. This means that whenever you see a getter or setter method in a core class, you can invoke it just as you would call a property.
The Getter method is a method whose name begins with Get, and the setter method name begins with a set. The name of the property is defined by the part of the method name that follows get or set. As shown in the following code, the Getter Method Getlabel () and the Setter Method SetLabel () manipulate the Label property:
Namespace App\components;use yii\base\object;class Foo extend object{ private $_label; Public Function Getlabel () { return $this->_label; } Public Function SetLabel ($value) { $this->_label = Trim ($value); }}
(explained in detail: Getter and Setter methods create a property named Label, which in this case points to a private internal attribute, _label. )
Getter/setter defines the same attribute usage as class member variables. The main difference is that when this property is read, the corresponding getter method is called, and when the property is assigned, the corresponding setter method is called. Such as:
Equivalent to $label = $object->getlabel (), $label = $object->label;//equivalent to $object->setlabel (' abc '); $object->label = ' abc ';
A property that only defines a getter without a setter is a read-only property. Attempting to assign a value to such a property will result in an yii\base\invalidcallexception (invalid invocation) exception. Similarly, a property that is defined only by the setter method and without the Getter method is a write-only property, and attempting to read this property also triggers an exception. There are few cases where write-only attributes are used.
attributes defined by getter and setter also have some special rules and restrictions:
The names of such properties are case-insensitive. For example, $object->label and $object->label are the same property. Because the PHP method name is case insensitive.
If such property names and class member variables are the same, the future will prevail. For example, suppose the above Foo class has a label member variable and then assigns $object->label = ' abc ' to the member variable instead of the setter SetLabel () method.
This type of property does not support visibility (access restrictions). The getter and setter methods that define a property have no effect on the visibility of the property, whether public, protected, or private.
Getter and setter methods of such attributes can only be defined as non-static, and are not treated in the same way if they are defined as static methods.
Back to the question at the beginning, instead of calling the trim () function everywhere, now we only need to call it once within the setter SetLabel () method. If the new requirement to capitalize the first letter of the label comes, we only need to modify the SetLabel () method without touching any other code.
Steps to implement a property
We know that __get () __set () is automatically called when a nonexistent member variable is read and written to the object. Yii is using this to provide support for attributes. As you can see from the code above, if you access a property of an object, Yii invokes a function named Get property name (). For example, Someobject->foo, Someobject->getfoo () is called automatically. If you modify a property, the corresponding setter function is called. For example, Someobject->foo = $someValue, Someobject->setfoo ($someValue) is called automatically.
Therefore, there are typically three steps to implement a property:
Inherit from Yii\base\object.
Declares a private member variable that is used to hold the property.
Provides getter or setter functions, or both, for accessing and modifying the private member variables mentioned above. If only the getter is provided, the property is read-only and only the setter is provided, or write-only.
The following Post class implements a readable writable property, title:
Class Post extends Yii\base\object //First step: Inherit from yii\base\object{ private $_title; Step two: Declare a private member variable public function GetTitle () //Third step: Provide getter and setter { return $this->_title; } Public Function Settitle ($value) { $this->_title = Trim ($value); }}
Theoretically speaking, the private $_title as public $title, but also can achieve $post->title read and write. But this is not a good habit, for the following reasons:
The encapsulation of the class has been lost. In general, it is a good programming habit for member variables to be invisible to the outside. From here you may not see, but if one day, you do not want users to change the title, how do you change? How do I ensure that the title is not directly modified in the code? If a setter is provided, simply deleting the setter will throw an exception if there is no clean-up write to the header. Using the public $title method, you change to private $title can troubleshoot the write exception, but the read is also forbidden.
For the title write, you want to remove the space. Using the setter method, you simply need to call trim () in this place just like the code snippet above. However, if you use the public $title method, there is no doubt that every write statement calls Trim (). Can you guarantee that there is not a single omission?
Therefore, using the public $title is just a quick, seemingly simple, but future modification is a hassle. It's a nightmare, you can say. This is the meaning of software engineering, through a certain method, so that the code is easy to maintain, easy to modify. Looking at the moment as if not necessary, but actually ate a loss of friends or by the customer's boss forced to modify the code written by a programmer, greet his loved ones, will feel that this is very necessary.
But there is no absolute. Because __get () and __set () are called when they traverse all member variables and cannot find a matching member variable. As a result, its efficiency is inherently lower than the use of member variables. In some simple cases of representing data structures, data sets, etc., and without the need for read and write control, you can consider using member variables as attributes, which can improve your efficiency.
Another trick to improve efficiency is to use $pro = $object->getpro () instead of $pro = $object->pro and $objcect->setpro ($value) instead of $object->p Ro = $value. This is functionally the same effect, but avoids the use of __get () and __set (), which is equivalent to bypassing the traversal process.
It is estimated that someone should scold me, Yii finally realized the mechanism of properties, is to facilitate the developers, but I am here to teach you how to use the original way to improve the so-called efficiency. Well, it is true that there is a certain contradiction between the convenience of development and the efficiency of execution. My personal point of view is more inclined to convenience first, use well, with foot yii to create the convenience conditions for us. As for the efficiency of the matter, more is the framework itself needs to be noted, we just do not write extra 2 of the code is OK.
You can rest assured, however, that in Yii's framework, there are very few code $app->request, but $app->getrequest () is used. In other words, the framework itself is particularly focused on efficiency, and as for convenience, it is left to developers. In short, here is just point out there is such a knowledge point, as to use, how to use, completely depends on you.
It is worth noting that:
Because the time to automatically call __get () __set () occurs only when a member variable that does not exist is accessed. Therefore, if the member variable public is defined $title then, even if GetTitle () Settitle () is defined, they will not be called. Because $post->title, it points directly to the Pulic $title, __get () __set () is not called. It was cut off from the root.
Because PHP is case insensitive to class methods, that is, case insensitive, $post->gettitle () and $post->gettitle () are called the same functions. Therefore, $post->title and $post->title are the same property. That is, property names are also case insensitive.
Since __get () __set () are public, it is meaningless to declare getTitle () Settitle () as public, private, protected, and externally accessible. Therefore, all properties are public.
Because __get () __set () is not static, there is no way to use the static property.
Other property-related methods of object
In addition to __get () __set (), Yii\base\object provides the following methods to facilitate the use of attributes:
__isset () is used to test whether the property value is not null and is called automatically when Isset ($object->property). Note that the property has a corresponding getter.
__unset () is used to set the property value to null, which is called automatically when unset ($object->property). Note that the attribute should have a corresponding setter.
Hasproperty () is used to test for a property. That is, a getter or setter is defined. If the parameter of Hasproperty () $checkVars = True (by default, True), then the member variable with the same name is considered to have that property, as the public $title mentioned earlier.
Cangetproperty () tests whether a property is readable and the meaning of the parameter $checkVars is the same as above. Properties can be read as long as the getter is defined. Also, if $checkVars is true. As long as the class defines a member variable, public, private, or protected, it is considered to be readable.
Cansetproperty () tests if a property is writable, and the parameter $checkVars is the same as the meaning. Attributes can be written as long as the setter is defined. At the same time, the $checkVars is ture. So long as the class defines a member variable, whether it is public, private, or protected, it is considered writable.
Object and Component
Yii\base\component inherits from Yii\base\object, so he also has basic functions such as attributes.
However, since Componet also introduces events and behaviors, it does not simply inherit the property implementation of object, but rather overloads the functions such as __get () __set (), based on the same mechanism. But from the implementation mechanism, it is the same. This does not affect understanding.
As mentioned earlier, the authorities are positioning yii in a component-based framework. The concept of visible components is the foundation of Yii. If you are interested in reading Yii's source code or API documentation, you will find that almost all of Yii's core classes are born (inherited from) yii\base\component.
In the Yii1.1, there is already a component, then is ccomponent. Yii2 splits the ccomponent in Yii1.1 into two classes: Yii\base\object and yii\base\component.
The object is lightweight, and the property of the class is defined by getter and setter. Component is derived from object and supports events (event) and behavior (behavior). As a result, the component class has three important features:
Attribute (property)
Events (Event)
Behavior (behavior)
I believe you know more or less that these three traits are important entry points for enriching and expanding class functions and changing class behavior. As a result, component's position in Yii is extremely high.
While providing more features and more convenience, component has increased the two features of event and behavior, and at the same time, it has sacrificed some efficiency while facilitating development. If you do not need to use both the event and behavior features in development, such as classes that represent some data. Then, you can inherit from an object without inheriting from component. A typical scenario is if you represent a set of data entered by the user, then use object. If you need to deal with the behavior of the object and the events that can respond to it, there is no doubt that component should be used. In terms of efficiency, object is closer to the native PHP class, so if possible, use object as a priority.
Related recommendations:
Yii operating mechanism and routing details
How Yii hides URLs in index.php
Summary of database query operation in PHP YII framework