Yii2 framework backup
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

3.2 KiB

Components

Components are the main building blocks in Yii applications. Components are instances of yii\base\Component or it child class. They support features such as properties, events and behaviors, which makes them more customizable and easier to use. For example, you may use the included yii\jui\DatePicker, a user interface component, 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',
    ],
]);

While components are very powerful, they are a bit heavier compared to normal objects, due to the fact that it takes extra memory and CPU time in order to support events and behaviors. If your components do not need these two features, you may consider extending your component class from yii\base\Object instead of yii\base\Component, which will make your components as efficient as normal objects, but with the extra support for properties.

When extending your class from yii\base\Component or yii\base\Object, it is recommended that you follow these conventions:

  • If you override the constructor, specify a $config parameter as its last parameter and pass this parameter to the parent constructor.
  • Call parent constructor at the end of the constructor.
  • If you override the yii\base\Object::init() method, make sure you call the parent implementation.

For example,

namespace 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
    }
}

This will make your components configurable when they are being 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 call of Yii::createObject() looks more complicated, it is more powerful due to the fact that it is implemented on top of a dependency injection container.

The yii\base\Object class enforces the following object lifecycle:

  1. Pre-initialization within constructor. You can set default property values here.
  2. Configuring object with $config. The configuration may overwrite the default values set above.
  3. Post-initialization within yii\base\Object::init(). You may override this method and do sanity check and normalization of the properties.
  4. Object method calls.

The first three steps all happen within the object constructor. This means, once you get an object instance, it has already been initialized to a proper state that you can work on.