Components are the main building blocks of Yii applications. Components are instances of yii\base\Component, or an extended class. The three main features that components provide to other classes are:
Separately and combined, these features make Yii classes much more customizable and easier to use. For example, the included yii\jui\DatePicker, a user interface component, can be used in a view to generate an interactive date picker:
use yii\jui\DatePicker;
echo DatePicker::widget([
'language' => 'ru',
'name' => 'country',
'clientOptions' => [
'dateFormat' => 'yy-mm-dd',
],
]);
The widget's properties are easily writable because the class extends yii\base\Component.
While components are very powerful, they are a bit heavier than normal objects, due to the fact that it takes extra memory and CPU time to support event and behavior functionality in particular. If your components do not need these two features, you may consider extending your component class from yii\base\BaseObject instead of yii\base\Component. Doing so will make your components as efficient as normal PHP objects, but with added support for properties.
When extending your class from yii\base\Component or yii\base\BaseObject, it is recommended that you follow these conventions:
$config
parameter as the constructor's last parameter, and then pass this parameter
to the parent constructor.init()
at the beginning of your init()
method.For example:
<?php
namespace yii\components\MyClass;
use yii\base\BaseObject;
class MyClass extends BaseObject
{
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
}
}
Following these guidelines will make your components configurable when they are created. For example:
$component = new MyClass(1, 2, ['prop1' => 3, 'prop2' => 4]);
// alternatively
$component = \Yii::createObject([
'class' => MyClass::className(),
'prop1' => 3,
'prop2' => 4,
], [1, 2]);
Info: While the approach of calling Yii::createObject() looks more complicated, it is more powerful because it is implemented on top of a dependency injection container.
The yii\base\BaseObject class enforces the following object lifecycle:
$config
. The configuration may overwrite the default values set within the constructor.The first three steps all happen within the object's constructor. This means that once you get a class instance (i.e., an object), that object has already been initialized to a proper, reliable state.
Found a typo or you think this page needs improvement?
Edit it on github !
Signup or Login in order to comment.