Home >PHP Framework >YII >The difference between yii events and behaviors

The difference between yii events and behaviors

(*-*)浩
(*-*)浩Original
2019-12-04 11:04:213567browse

Personally, in Yii, the most difficult things to understand are events and behaviors. This is not only because their concepts are difficult to understand, but also because their application scenarios are difficult to understand. It is not clear when events and behaviors should be used for development.

The difference between yii events and behaviors

Event

The event model is the "observer pattern" in the design pattern: when the object When the state changes, this object can notify other objects of the event.

In order to use the event model, these three steps are required: (Recommended learning: yii frames )

## 1, define event; 2. Register event handle; 3. Trigger event.

Why do we need to do these three steps? Because for PHP itself, its execution process is not run in a procedural manner,

so Yii's event triggering mechanism will not directly trigger events like ActionScript 3.

Some people say that the event concept of Yii is similar to the event concept in js, because Yii binds events to the execution process of Yii::app().

Behavior

Here we first re-describe why we should use behavior.

There are two ways to add features to a class:

1. Directly modify the code of this class and add some member functions and member variables;

2. Derive and extend through subclasses.

Obviously the second method is easier to maintain and expand. However, if multiple features need to be added to a class (multiple people at different times), then multi-level derivation is required, which obviously increases maintenance costs.

In Yii, through behavioral class binding, the component adds one or more member methods and member variables of the CBeavior class to itself, and unloads some CBeavior classes when it is not needed.

At the same time, you can override the CBeavior::events method to achieve multiple event bindings to the target class. These events will be bound together when the current behavior is bound to the target class.

Yii2 event and behavior summary

1) Events are used to trigger the execution of a preset piece of code at a specific point in time. You need to use the event class , all inherited from yii\base\Component.

2) Behavior is used to supplement the class. By binding the behavior to a class, the class can have the properties and methods defined by the behavior itself, just like the class itself has these properties and methods. .

And there is no need to write a new class to inherit or include existing classes.

The above is the detailed content of The difference between yii events and behaviors. 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