Component is the main component of the YII framework application (application). It is an instance or subclass of [yii\base\Component]. Components have three important characteristics (functions):
##·Properties
·Events
·Behaviors
Using these features alone or in combination can make the classes of the Yii framework become more Easy to use and customizable. For example, by introducing a user interaction component [yii\jui\DataPicker\data picker wigets], you can easily generate a date selection control (jquery ui date picker) in the view.use yii\jui\DatePicker; echo DatePicker::widget([ 'language' => 'ru', 'name' => 'country', 'clientOptions' => [ 'dateFormat' => 'yy-mm-dd', ], ]);
·Assuming that you rewrite the constructor, you must define a parameter $config as the last parameter of the constructor. This parameter is passed to the constructor of the parent class for use.
·The constructor of the parent class must be called at the end of the overridden constructor
·Suppose you rewrite [yii\base\Object: :init()|init()] method, then the init method of the parent class must be called starting from the overridden init method
For examplenamespace yii\components\MyClass; use yii\base\Object; class MyClass extends Object { public $prop1; public $prop2; public function __construct($param1, $param2, $config = []) { // ... initialization before configuration is applied parent::__construct($config); } public function init() { parent::init(); // ... initialization after configuration is applied } }
$component = new MyClass(1, 2, ['prop1' => 3, 'prop2' => 4]); // alternatively $component = \Yii::createObject([ 'class' => MyClass::className(), 'prop1' => 3, 'prop2' => 4, ], [1, 2]);
Yii introductory tutorials, everyone is welcome to learn!
The above is the detailed content of What does component mean in yii2?. For more information, please follow other related articles on the PHP Chinese website!