Home PHP Framework YII What does component mean in yii2?

What does component mean in yii2?

Dec 09, 2019 am 11:15 AM
yii2 components

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

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
    }
}
Copy after login

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]);
Copy after login

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 of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to install the Windows 10 old version component DirectPlay How to install the Windows 10 old version component DirectPlay Dec 28, 2023 pm 03:43 PM

Many users always encounter some problems when playing some games on win10, such as screen freezes and blurred screens. At this time, we can solve the problem by turning on the directplay function, and the operation method of the function is also Very simple. How to install directplay, the old component of win10 1. Enter "Control Panel" in the search box and open it 2. Select large icons as the viewing method 3. Find "Programs and Features" 4. Click on the left to enable or turn off win functions 5. Select the old version here Just check the box

How to implement calendar component using Vue? How to implement calendar component using Vue? Jun 25, 2023 pm 01:28 PM

Vue is a very popular front-end framework. It provides many tools and functions, such as componentization, data binding, event handling, etc., which can help developers build efficient, flexible and easy-to-maintain Web applications. In this article, I will introduce how to implement a calendar component using Vue. 1. Requirements analysis First, we need to analyze the requirements of this calendar component. A basic calendar should have the following functions: display the calendar page of the current month; support switching to the previous month or next month; support clicking on a certain day,

VUE3 development basics: using extends to inherit components VUE3 development basics: using extends to inherit components Jun 16, 2023 am 08:58 AM

Vue is one of the most popular front-end frameworks currently, and VUE3 is the latest version of the Vue framework. Compared with VUE2, VUE3 has higher performance and a better development experience, and has become the first choice of many developers. In VUE3, using extends to inherit components is a very practical development method. This article will introduce how to use extends to inherit components. What is extends? In Vue, extends is a very practical attribute, which can be used for child components to inherit from their parents.

Let's talk about how Vue dynamically renders components through JSX Let's talk about how Vue dynamically renders components through JSX Dec 05, 2022 pm 06:52 PM

How does Vue dynamically render components through JSX? The following article will introduce to you how Vue can efficiently dynamically render components through JSX. I hope it will be helpful to you!

Angular components and their display properties: understanding non-block default values Angular components and their display properties: understanding non-block default values Mar 15, 2024 pm 04:51 PM

The default display behavior for components in the Angular framework is not for block-level elements. This design choice promotes encapsulation of component styles and encourages developers to consciously define how each component is displayed. By explicitly setting the CSS property display, the display of Angular components can be fully controlled to achieve the desired layout and responsiveness.

How to open the settings of the old version of win10 components How to open the settings of the old version of win10 components Dec 22, 2023 am 08:45 AM

Win10 old version components need to be turned on by users themselves in the settings, because many components are usually closed by default. First we need to enter the settings. The operation is very simple. Just follow the steps below. Where are the win10 old version components? Open 1. Click Start, then click "Win System" 2. Click to enter the Control Panel 3. Then click the program below 4. Click "Enable or turn off Win functions" 5. Here you can choose what you want to open

Vue component development: implementation method of progress bar component Vue component development: implementation method of progress bar component Nov 24, 2023 am 08:56 AM

Vue component development: Progress bar component implementation method Preface: In Web development, the progress bar is a common UI component, often used to display the progress of operations in scenarios such as data requests, file uploads, and form submissions. In Vue.js, we can easily implement a progress bar component by customizing components. This article will introduce an implementation method and provide specific code examples. I hope it will be helpful to Vue.js beginners. Component structure and style First, we need to define the basic structure and style of the progress bar component.

How to remove jquery in yii2 How to remove jquery in yii2 Feb 17, 2023 am 09:55 AM

How to remove jquery from yii2: 1. Edit the AppAsset.php file and comment out the "yii\web\YiiAsset" value in the variable $depends; 2. Edit the main.php file and add the configuration "'yii" under the field "components" \web\JqueryAsset' => ['js' => [],'sourcePath' => null,]," to remove the jquery script.

See all articles