Home > Article > Backend Development > Yii source code interpretation - attributes
Yii Basics
Attributes property
Attributes are used to represent the status of a class. From the perspective of access, there is no difference between attributes and member variables. However, member variables are a concept in terms of the structural composition of the class, while attributes are a concept in terms of the functional logic of the class
Q: What is the difference between attributes and member variables?
Member variables are an "internal" concept that reflect the structure of the class. Attribute is an "external" concept, reflecting the logical meaning of the class.
Member variables do not have read and write permission controls, while properties can be specified as read-only or write-only, or both read and write.
Member variables do not undergo any post-processing for reading and do not undergo any pre-processing for writing, while attributes do.
The public member variable can be regarded as a readable and writable property without any preprocessing or postprocessing. Since private member variables are not visible from the outside and are inconsistent with the "external" characteristics of attributes, they cannot be regarded as attributes.
Although in most cases, attributes are represented by one or more member variables, there is no necessary correspondence between attributes and member variables. For example, the output attribute of the NAND gate does not have a so-called $output member. The variables correspond to it.
provides support for attributes by
yiibaseObject
implementation of attributes
<code>class foo extends yii\base\object{ private $_title; public function setTitle($title){ $this->_title = trim($title); } public function getTitle(){ return $this->_title; } }</code>
attributes can achieve better encapsulation of classes, while component entries and unified management of member variables.
However, __get()
, __set()
traverses all member variables and is called only when no matching member variable is found. 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, you can consider using member variables as attributes, which can improve efficiency.
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 when $post->title, it will point directly to the public $title.
PHP is not case-sensitive for class methods, that is, it is not case-sensitive, and it is also case-insensitive for attribute names.
__get() __set() are all public,
static
methods are not easy to use.
Component
Yii claims to be a component-based framework.
yiibaseComponent
inherits from yiibaseObject
. Component overloads the property methods in Object and also adds events and behaviors.
Since overloading adds events and behaviors, the performance of Component is slightly worse than that of Object.
Object configuration
Yii’s object configuration method is unified. The configuration of all objects is configured through __construct()
in Object
. The essence of configuration lies in Yii::configure()
, which constructs properties by configuring arrays.
Q: What if the configuration is a multi-dimensional array?
Yii implements the specific processing of this array in
yiidiServiceLocator
:setComponents
.
Summary
Through yiibaseObject::__construct()
, we can see that all objects, including the loading of Component properties, have 4 stages (the first three stages are integrated in one go).
Pre-initialization: The default definition of Property
Yii::configure()
to load the configuration array, override the Property
and then initialize: init()
execution
class method call stage.
Reference
http://www.digpage.com/
The above introduces Yii source code interpretation-attributes, including aspects of content. I hope it will be helpful to friends who are interested in PHP tutorials.