Components are the main building blocks of Yii applications. (Recommended learning: yii tutorial)
is an instance of the yii\base\Component class or its subclass. The three main functions used to distinguish it from other classes are:
Property
Event
Behavior
Either used alone or in conjunction with each other, the application of these functions makes Yii classes more flexible and easier to use. Take the widget yii\jui\DatePicker as an example. This is a UI component that facilitates you to generate an interactive date picker in a view:
use yii\jui\DatePicker; echo DatePicker::widget([ 'language' => 'zh-CN', 'name' => 'country', 'clientOptions' => [ 'dateFormat' => 'yy-mm-dd', ], ]);
This widget inherits from yii\base\Component, and its various Item properties can be easily overridden.
It is precisely because of the powerful functions of components that they are slightly heavier than regular objects (Object) because they use additional memory and CPU time to process events and behaviors.
If you don’t need these two functions, you can inherit yii\base\Object instead of yii\base\Component. In this way, the component can be as efficient as a normal PHP object, while also supporting the property function.
When inheriting yii\base\Component or yii\base\Object, it is recommended that you use the following coding style:
If you need to override the constructor (Constructor ), pass in $config as the last parameter of the constructor method, and then pass it to the constructor of the parent class.
Always call the parent class's constructor at the end of your overridden constructor.
If you override the yii\base\Object::init() method, please make sure you call the parent class's init method at the beginning of the init method.
The above is the detailed content of where are yii components. For more information, please follow other related articles on the PHP Chinese website!