Home >Backend Development >PHP Problem >what are php actions and events

what are php actions and events

angryTom
angryTomOriginal
2019-11-05 17:36:293433browse

what are php actions and events

What are php behaviors and events?

Behavior

Behavior (Behavior) is a ThinkPHP extension One of the key extensions in the mechanism is that behaviors can be called independently or bound to a tag for listening.

Let’s first re-describe why behaviors are used.

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, through subclass to extend.

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.

Event

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

In order to use the event model, these three steps need to be implemented: 1. Define the event; 2. Register the event handler; 3. Trigger the event.

Why do we need to do these three steps? Because for PHP itself, its execution process is not run by process.

For more PHP related knowledge, please visit PHP Chinese website!

The above is the detailed content of what are php actions and events. 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