Definition and binding method of behavior in the Yii framework of PHP, yii framework
Define Behavior
To define Behavior, you can create a class by inheriting yii \ base \ Behavior or its subclass. For example:
namespace app\components;use yii\base\Behavior;class MyBehavior extends Behavior{ public $prop1; private $_prop2; public function getProp2() { return $this->_prop2; } public function setProp2($value) { $this->_prop2 = $value; } public function foo() { // ... }}
The code above defines the behavior class app \ components \ MyBehavior and provides two attributes prop1, prop2, and a method foo () for the component to attach the behavior (). Note that the property prop2 is defined by getter getProp2 () and setter setProp2. This can be used because yii \ base \ Object is the ancestor class of yii \ base \ Behavior. This ancestor class supports defining attributes using the getter and setter methods.
Tip: You can use the yii \ base \ Behavior: owner attribute to access the components that have been attached to the Behavior.
Static Method binding
For static binding, you only need to reload yii \ base \ Component: behaviors. This method is used to describe the behavior of a class. How to describe it? The configuration can be the Behavior class name or the Behavior class configuration array:
Namespace app \ models; use yii \ db \ ActiveRecord; use app \ Components \ MyBehavior; class User extends ActiveRecord {public function behaviors () {return [// anonymous behavior, only the class name MyBehavior: className () and // The behavior name myBehavior2 are provided. The class name 'mybehavior2' => MyBehavior: className (), // anonymous behavior, which provides the configuration array of the MyBehavior class ['class' => MyBehavior: className (), 'prop1' => 'value1 ', 'prop3' => 'value3 ',], // The behavior named myBehavior4 is also the configuration array of the MyBehavior class 'mybehavior4' => ['class' => MyBehavior :: className (), 'prop1' => 'value1', 'prop1' => 'value3 ',];}
Another static binding method is to bind the configuration file:
[ 'as myBehavior2' => MyBehavior::className(), 'as myBehavior3' => [ 'class' => MyBehavior::className(), 'prop1' => 'value1', 'prop3' => 'value3', ],]
Dynamic Method binding
Dynamic binding behavior. You need to call yii \ base \ Compoent: attachBehaviors ():
$ Component-> attachBehaviors (['mybehavior1' => new MyBehavior, // This is a naming behavior MyBehavior: className (), // This is an anonymous Behavior]);
This method accepts an array parameter, which has the same meaning as the above static binding behavior.
In the preceding examples, the array key is used as the behavior name. If the key name is not provided, the behavior is anonymous.
For the naming behavior, you can call yii \ base \ Component: getBehavior () to obtain the bound behavior:
$behavior = $Component->getBehavior('myBehavior2');
There is no way to directly reference anonymous behaviors. However, you can obtain all bound behaviors:
$behaviors = $Component->getBehaviors();
Internal principles of binding
Just reload a yii \ base \ Component: behaviors () to use the behavior in such a magical way? This is only the tip of the iceberg. In fact, it is related to the binding process. The relevant aspects include:
yii\base\Component::behaviors()yii\base\Component::ensureBehaviors()yii\base\Component::attachBehaviorInternal()yii\base\Behavior::attach()
Behavior only occupies one of the four methods. More code is completed in Component.
Yii \ base \ Component: behaviors () as mentioned above when binding static methods, an array is returned to describe the behavior. What about yii \ base \ Component: ensuerBehaviors?
This method will call _ get () _ set () _ isset () _ unset () _ call () canGetProperty () hasMethod () in many places of Component () hasEventHandlers () on () off () and so on, is it a headache to see so much? It is not complicated at all. In a word, as long as the class attributes, methods, and events are involved, this function will be called.
Who are the people who need ensureBehaviors? As the name indicates, its role is "ensure ". In fact, it is only to ensure that the behavior described in behaviors () has been bound:
Public function ensureBehaviors () {// if it is null, it indicates that it has not been bound. // a null array indicates that no behavior is bound. if ($ this-> _ behaviors = null) {$ this-> _ behaviors = []; // traverses the array returned by $ this-> behaviors () and binds the foreach ($ this-> behaviors () as $ name =>$ behavior) {$ this-> attachBehaviorInternal ($ name, $ behavior );}}}
This method is mainly used for sub-classes. yii \ base \ Compoent has no pre-injection behavior, so this call is useless. However, for subclasses, you may overload yii \ base \ Compoent: behaviros () to inject some behavior in advance. Then, this function will inject these actions first.
From the code above, we naturally see the third thing to be said next, yii \ base \ Component \ attachBehaviorInternal ():
Private function attachBehaviorInternal ($ name, $ behavior) {// It is not a Behavior instance, but a class name and an array of configurations. Create it if (! ($ Behavior instanceof Behavior) {$ behavior = Yii: createObject ($ behavior);} // anonymous behavior if (is_int ($ name )) {$ behavior-> attach ($ this); $ this-> _ behaviors [] = $ behavior; // naming behavior} else {// an action with the same name already exists, you need to unbind the new behavior first. If (isset ($ this-> _ behaviors [$ name]) {$ this-> _ behaviors [$ name]-> detach ();} $ behavior-> attach ($ this); $ this-> _ behaviors [$ name] = $ behavior;} return $ behavior ;}
Note that this is a private member. In Yii, all methods suffixed with * Internal are private. This method has done the following:
If the $ behavior parameter is not a Behavior instance, use Yii: createObject () as the parameter to create it.
If the behavior is bound as an anonymous behavior, the behavior is directly attached to this class.
If it is a naming action, first check whether there are behaviors with the same name already bound to this class. If so, replace the previous behavior with the behavior later.
In yii \ base \ Component: attachBehaviorInternal (), yii \ base \ Behavior: attach () is called with $ this as the parameter (). This leads to the last bind-related guy yii \ base \ Behavior: attach (), which was not completed when we talked about the elements of Behavior. First look at the Code:
public function attach($owner){ $this->owner = $owner; foreach ($this->events() as $event => $handler) { $owner->on($event, is_string($handler) ? [$this, $handler] : $handler); }}
The above Code does two things:
- Set the $ owner of the behavior so that the behavior can access and operate on the objects attached to it.
- Traverses the array returned by events () in the action and binds the event to be responded to by the on () of the dependent class to the class.
Summary
After talking about the binding, let's make a summary:
- The bound action is initiated from Component;
- Static binding is implemented by reloading yii \ base \ Componet: behaviors;
- Dynamic binding is implemented by calling yii \ base \ Component: attachBehaviors;
- Behavior can also be bound by configuring the as configuration item for Component;
- There are two behavior types: anonymous behavior and naming behavior. The difference is whether a name is given during binding. The naming behavior can be identified by its name, so as to perform specific operations such as release;
- During the binding process, the bound behavior will replace the bound behavior with the same name;
- Binding has two meanings: one is to set $ owner for the behavior. Second, bind the handler of the event to be returned in the action to the class.
Articles you may be interested in:
- How to remove the behavior bound to a component from the Yii framework of PHP
- Detailed description of Behaviors in PHP Yii framework
- In-depth explanation of attributes in PHP Yii framework)
- Tutorial on using database configuration and SQL operations in PHP Yii framework
- In-depth analysis of event mechanism in PHP Yii framework
- A comprehensive explanation of the log function in the PHP Yii framework
- Yii Use find findAll to find the implementation of the specified field
- Parsing the addition, deletion, query, and modification of the yii Database
- Yii PHP Framework practical getting started tutorial (details)
- Describes the property injection and method injection of component behavior in the Yii framework of PHP.