Home > Article > Backend Development > In-depth explanation of properties (Property) in PHP's Yii framework_php skills
In PHP, member variables of a class are also called properties. They are part of the class definition and are used to represent the state of an instance (that is, to distinguish between different instances of the class). In specific practice, we often want to use a slightly special method to read and write attributes. For example, if you need to perform trim operation on the label attribute every time, you can use the following code to achieve it:
$object->label = trim($label);
The disadvantage of the above code is that whenever the label attribute is modified, the trim() function must be called again. If you need to handle the label attribute in other ways in the future, such as capitalizing the first letter, you will have to modify all the code that assigns values to the label attribute. This duplication of code can lead to bugs, and this practice obviously needs to be avoided whenever possible.
To solve this problem, Yii introduces a base class called yiibaseObject, which supports defining properties based on getter and setter (reader and setter) methods within the class. If a class needs to support this feature, it only needs to inherit yiibaseObject or its subclasses.
Supplement: Almost every core class of Yii framework inherits from yiibaseObject or its subclasses. This means that whenever you see a getter or setter method in a core class, you can call it just like a property.
Getter methods are methods whose names begin with get, while setter method names begin with set. The part after get or set in the method name defines the name of the property. As shown in the following code, the getter method getLabel() and the setter method setLabel() operate on the label attribute:
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); } }
(Explanation: The getter and setter methods create a property called label, which in this case points to a private internal property _label.)
The properties defined by getters/setters are used in the same way as class member variables. The main difference between the two is that when this property is read, the corresponding getter method will be called; and when the property is assigned a value, the corresponding setter method will be called. Such as:
// 等效于 $label = $object->getLabel(); $label = $object->label; // 等效于 $object->setLabel('abc'); $object->label = 'abc';
Properties that only define getters and no setters are read-only properties. Attempting to assign to such a property will result in a yiibaseInvalidCallException (invalid call) exception. Similarly, properties defined with only setter methods but no getter methods are write-only properties, and attempts to read such properties will also trigger an exception. There are almost no cases for using write-only properties.
Properties defined via getters and setters also have some special rules and restrictions:
The names of such attributes are not case-sensitive. For example, $object->label and $object->Label are the same property. Because PHP method names are not case-sensitive.
If the name of such an attribute is the same as that of a class member variable, the latter shall prevail. For example, suppose the above Foo class has a label member variable, and then assigning a value to $object->label = 'abc' will be assigned to the member variable instead of the setter setLabel() method.
Visibility (access restrictions) are not supported for this type of property. Whether a property's getter and setter methods are public, protected, or private has no effect on the property's visibility.
The getter and setter methods of such properties can only be defined as non-static. If they are defined as static methods (static), they will not be processed in the same way.
Going back to the question mentioned at the beginning, instead of calling the trim() function everywhere, now we only need to call it once within the setter setLabel() method. If a new requirement comes that the first letter of the label becomes capitalized, we only need to modify the setLabel() method without touching any other code.
Steps to implement attributes
We know that when reading and writing a non-existent member variable of an object, __get() __set() will be automatically called. Yii takes advantage of this to provide support for attributes. From the above code, you can see that if you access a certain property of an object, Yii will call the function named getpropertyname(). For example, SomeObject->Foo will automatically call SomeObject->getFoo(). If a property is modified, the corresponding setter function will be called. For example, SomeObject->Foo = $someValue will automatically call SomeObject->setFoo($someValue).
Therefore, to implement properties, there are usually three steps:
The following Post class implements the readable and writable attribute title:
class Post extends yii\base\Object // 第一步:继承自 yii\base\Object { private $_title; // 第二步:声明一个私有成员变量 public function getTitle() // 第三步:提供getter和setter { return $this->_title; } public function setTitle($value) { $this->_title = trim($value); } }
Theoretically speaking, writing private $_title as public $title can also read and write $post->title. But this is not a good habit, and here’s why:
The encapsulation of the class is lost. Generally speaking, it is a good programming practice to make member variables invisible to the outside world. You may not see it from here, but if one day you don’t want users to modify the title, how do you change it? How to ensure that the title is not modified directly in the code? If a setter is provided, as long as the setter is deleted, an exception will be thrown if there are uncleaned writes to the header. If you use the public $title method, you can check writing exceptions by changing it to private $title, but reading is also prohibited.
For title writing, you want to remove the spaces. Using the setter method, you only need to call trim() at this place like the above code snippet. But if you use the public $title method, then there is no doubt that trim() will be called on every write statement. Can you guarantee that nothing is missing?
Therefore, using public $title is only quick and seems simple, but it will be troublesome to modify in the future. It can be said to be a nightmare. This is the meaning of software engineering, making the code easy to maintain and modify through certain methods. It may seem unnecessary at first, but in fact, friends who have suffered losses or were forced by the client's boss to modify the code written by the previous programmer and greeted his relatives will all feel that this is very necessary.
However, there are no absolutes in this world. Since __get() and __set() are traversed through all member variables, they are called only when no matching member variable is found. Therefore, its efficiency is inherently lower than using member variables. In some simple situations where data structures, data collections, etc. are represented, and read-write control is not required, member variables can be considered as attributes, which can improve efficiency.
Another little trick to improve efficiency is: use $pro = $object->getPro() instead of $pro = $object->pro and $objcect->setPro($value) instead of $ object->pro = $value . This has the exact same functional effect, but avoids the use of __get() and __set(), which is equivalent to bypassing the traversal process.
I think someone should scold me here. Yii finally implemented the attribute mechanism just for the convenience of developers. However, I am here to teach you how to use the original method to improve the so-called efficiency. Well, indeed, there is a certain contradiction between convenience of development and high efficiency of execution. My personal point of view is more inclined to put convenience first, and make good use of the convenient conditions Yii creates for us. As for efficiency, the framework itself needs to pay more attention to it. As long as we don't write any extra code, it will be fine.
But you can rest assured that in the Yii framework, code like $app->request rarely appears, instead $app->getRequest() is used. In other words, the framework itself pays special attention to efficiency, and convenience is left to developers. In short, here is just a point of knowledge. As for whether to use it and how to use it, it is completely up to you.
It is worth noting:
Because the timing of automatically calling __get() __set() only occurs when accessing non-existing member variables. Therefore, if the member variable public $title is defined, then even if getTitle() setTitle() is defined, they will not be called. Because $post->title will directly point to the pulic $title, __get() __set() will not be called. It was severed from the root.
Since PHP is not case-sensitive for class methods, $post->getTitle() and $post->gettitle() call the same function. Therefore, $post->title and $post->Title are the same property. That is, attribute names are also case-insensitive.
Since __get() and __set() are both public, it makes no sense whether getTitle() setTitle() is declared as public, private, or protected. It is also accessible from the outside. Therefore, all properties are public.
Since neither __get() __set() is static, there is no way to use static attributes.
Other property-related methods of Object
In addition to __get() __set(), yiibaseObject also provides the following methods to facilitate the use of attributes:
yiibaseComponent inherits from yiibaseObject, therefore, it also has basic functions such as properties.
However, since Component also introduces events and behaviors, it does not simply inherit the property implementation method of Object, but overloads functions such as __get() __set() based on the same mechanism. But in terms of implementation mechanism, they are the same. This does not affect understanding.
As mentioned before, Yii is officially positioned as a component-based framework. The concept of visible components is the foundation of Yii. If you are interested in reading the source code or API documentation of Yii, you will find that almost all core classes of Yii are derived from (inherited from) yiibaseComponent.
In Yii1.1, there was already a component, which was CComponent at that time. Yii2 splits CComponent in Yii1.1 into two classes: yiibaseObject and yiibaseComponent.
Among them, Object is relatively lightweight and defines the properties of the class through getters and setters. Component is derived from Object and supports events and behaviors. Therefore, the Component class has three important characteristics:
I believe you have more or less understood that these three features are important entry points for enriching and expanding class functions and changing class behavior. Therefore, Component has a very high status in Yii.
While providing more functions and convenience, Component has added the two features of event and behavior, which facilitates development while sacrificing a certain amount of efficiency. If you do not need to use the two features of event and behavior during development, such as a class that represents some data. Then, you can inherit from Object instead of Component. A typical application scenario is to use Object if it represents a set of data input by the user. And if you need to handle the behavior of the object and the events that can respond to the processing, there is no doubt that Component should be used. In terms of efficiency, Object is closer to the native PHP class, so when possible, Object should be used first.