In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
This article introduces to you what is the difference between Object and Component, the content is very detailed, interested friends can refer to, hope to be helpful to you.
Because Componet introduces events and behaviors, it does not simply inherit the property implementation of Object, but overloads functions such as _ _ get () _ set () based on the same mechanism. But in terms of implementation mechanism, it is the same. This does not affect understanding.
As mentioned earlier, Yii is officially positioned as a component-based framework. It can be seen that the concept of components is the foundation of Yii. If you are interested in reading the Yii source code or API documentation, you will find that almost all the core classes of Yii derive from (inherit from) yii\ base\ Component.
When I was in Yii1.1, I already had component. At that time, it was CComponent. Yii2 splits CComponent in Yii1.1 into two classes: yii\ base\ Object and yii\ base\ Component.
Among them, Object is more lightweight, defining the property of the class through getter and setter. Component is derived from Object and supports events (event) and behaviors (behavior). Therefore, the Component class has three important features: property, event, and behavior.
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 plays a very high role in Yii.
While providing more functions and convenience, Component due to the addition of event and behavior these two features, in the convenience of development, but also at the expense of some efficiency. If you don't need to use event and behavior in your development, such as classes that represent some data. Then instead of inheriting from Component, you can inherit from Object. A typical application scenario is to use Object if a set of data entered by the user is represented. If you need to handle the behavior of the object and the events that can be handled in response, there is no doubt that Component should be used. In terms of efficiency, Object is closer to the native PHP class, so Object should be preferred where possible.
The configuration method of Object
Yii provides a unified way to configure objects. This approach runs through the entire Yii. The configuration of the Application object is the embodiment of this configuration:
$config = yii\ helpers\ ArrayHelper::merge (
Require (_ _ DIR__. '/.. /.. / common/config/main.php')
Require (_ _ DIR__. '/.. /.. / common/config/main-local.php')
Require (_ _ DIR__. '/.. / config/main.php')
Require (_ _ DIR__. '/.. / config/main-local.php')
)
$application = new yii\ web\ Application ($config)
$config looks complex, but it is essentially an array of various configuration items. In Yii, objects are configured uniformly using arrays, and the key to all this is in the constructor defined by yii\ base\ Object:
Public function _ _ construct ($config = [])
{
If (! empty ($config)) {
Yii::configure ($this, $config)
}
$this- > init ()
}
The construction process for all yii\ base\ Object is as follows:
The builder is called automatically with the $config array as an argument.
The builder function calls Yii::configure () to configure the object.
Finally, the constructor calls the object's init () method for initialization.
The secret of the array configuration object is in Yii::configure (), but to put it bluntly, there is no magic:
Public static function configure ($object, $properties)
{
Foreach ($properties as $name = > $value) {
$object- > $name = $value
}
Return $object
}
The process of configuration is to iterate through the $config configuration array, taking the key of the array as the attribute name, and assigning the property of the object with the value of the corresponding array element. Therefore, the key points to achieve the unified configuration of Yii are:
Inherited from yii\ base\ Object.
Provide setter methods for object properties to handle the configuration process correctly.
If you need to overload the constructor, take $config as the last argument to the constructor and pass it to the parent constructor.
At the end of the overloaded constructor, be sure to call the parent constructor.
If the yii\ base\ Object::init () function is overloaded, be sure to call the parent class's init () at the beginning of the overloaded function.
As long as you implement the above points, you can make the classes you write can be configured in the way established by Yii. This brings a lot of convenience in the process of writing code.
Someone as smart as you will definitely mention, what if a configuration item in a configuration array is also an array? What if the property of an object is also an object, rather than a simple numeric value or string?
In fact, these two problems are of the same nature. It is common if the property of one object is another object, just as many Component are introduced in Application. As you'll see later, the request property in $app- > request is an object. Then, when configuring $app, you must configure to this reqeust object. Since request is also an object, its configuration follows the rules of Yii, that is, it is configured with an array. Therefore, the two problems mentioned above are in fact homogeneous.
So, how to achieve it? The secret lies in the setter function. Because when $app is configured, the Yii::configure () function is eventually called. Without distinguishing whether the configuration item is a simple numeric value or an array, this function directly uses $object- > $name = $value to complete the assignment of the property. Then, for object properties, the configuration value $value is an array in order to configure it correctly. You need to do the right thing on its setter function. The Yii application yii\ web\ Application relies on defining a special setter function to automatically process configuration items. For example, in the configuration file of Yii, we can see a configuration item components, which in general looks like this:
'components' = > [
'request' = > [
/ /! Insert a secret key in the following (if it is empty)-
/ / this is required by cookie validation
'cookieValidationKey' = >' v7mBbyeTV 4ls7t8UIqQ2IBO60jYroomwfdust U'
]
'user' = > [
'identityClass' = > 'common\ models\ User'
'enableAutoLogin' = > true
]
'log' = > [
'traceLevel' = > YII_DEBUG? 3: 0
'targets' = > [
[
'class' = > 'yii\ log\ FileTarget'
'levels' = > [' error', 'warning']
]
]
]
'errorHandler' = > [
'errorAction' = >' site/error'
]
]
This is a typical array of nested configurations. So how does Yii configure them? Smart you must have thought that Yii must have defined a setter function called setComponents. Of course, Yii does not put this function in yii\ web\ Application, but in the parent class yii\ di\ ServiceLocator. As to what ServiceLocator is, I will talk about it later in the service locator (ServiceLocator) section. All you need to know here is that it is the parent of Application and provides the setter method of the components attribute:
Public function setComponents ($components)
{
Foreach ($components as $id = > $component) {
$this- > set ($id, $component)
}
}
Here is a member function, $this- > set (), which is the method used by the service locator to register the service. Let's not talk about this for a while and leave it to the service locator (Service Locator) section. Now you just need to know that this function takes care of the components configuration item in the configuration file.
From the perspective of yii\ base\ Object::__construct (), all Object, including the properties of Component, go through these four phases:
Pre-initialization stage. This is the initial stage, where the default value of property can be set at the beginning of the constructor _ _ construct ().
Object configuration phase. That is, the constructor call Yii::configure ($this, $config) phase mentioned earlier. This phase can override the default values of the property set in the previous stage and supplement the parameters that have no default values, that is, required parameters. $config is usually passed in from external code or through a configuration file.
Post-initialization phase. That is, the constructor calls the init () member function. By writing code in init (), you can check the value set during the configuration phase and standardize the property of the class.
Class method invocation phase. The first three phases are inseparable and are called by the constructor of the class. That is to say, once a class is instantiated, it goes through at least the first three stages. At this point, the state of the object is determined and reliable, and there is no uncertain property. All properties are either default values or incoming configuration values, and if the incoming configuration is incorrect or conflicting, it is also checked and standardized. In other words, you can rest assured to use it.
About what the difference between Object and Component is shared here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.