Home  >  Article  >  PHP Framework  >  What does component mean in yii2?

What does component mean in yii2?

爱喝马黛茶的安东尼
爱喝马黛茶的安东尼Original
2019-12-09 11:15:493144browse

What does component mean in yii2?

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',
    ],
]);

The properties of the DatePicker pendant are easily modified because it inherits the class [yii\base\Componet]

Although the component is very powerful, its overhead is greater than that of ordinary class objects. , the reason is that components require additional memory and CPU time to process events and behaviors. If your component does not need the two characteristics (functions) of events and behaviors, you can directly inherit [yii\base\Object] , so that your component is consistent in performance (efficiency) with ordinary class objects and supports properties.

To write a class that inherits [yii\base\Component] or [yii\base\Object], you need to follow the following conventions:

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

Follow the following example to make your Properties can be configured (initialized) when the component is created:

$component = new MyClass(1, 2, ['prop1' => 3, 'prop2' => 4]);
// alternatively
$component = \Yii::createObject([
    'class' => MyClass::className(),
    'prop1' => 3,
    'prop2' => 4,
], [1, 2]);

Tip: Using [Yii::createOject()] seems more complicated, but because [Yii::createObject] is based on dependency injection implementation, so it is more powerful

[yii\base\Object] class enforces the following life cycle:

(1) Implement pre-initialization in the constructor, and you can set the default at this time Attribute value;

(2) Configure the object through $config, and the default value can be overridden through object configuration in the constructor;

(3) In [yii\base\Object::init ()|init()] method, perform initialization configuration. Routine checks and attribute normalization checks can be implemented in the init method;

(4) Object method call.

The first three stages are all implemented in the constructor, which means that when you get an instance of an object, it has been initialized to the appropriate state and can be used with confidence.

PHP Chinese website has a large number of free

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn