Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Introduction to the definition and binding method of behavior in PHP's Yii Framework

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains the "introduction to the definition and binding methods of behavior in PHP's Yii framework". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "the definition of behavior and binding methods in PHP's Yii framework".

Define behavior

To define the behavior, create a class by inheriting yii\ base\ Behavior or its subclasses. Such as:

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 above code defines the behavior class app\ components\ MyBehavior and provides two properties prop1, prop2, and a method foo () for the component to which you want to attach the behavior. Note that the attribute prop2 is defined by getter getProp2 () and setter setProp2 (). This can be used because yii\ base\ Object is the ancestor of yii\ base\ Behavior, which supports the definition of properties with getter and setter methods

Tip: the components that the behavior has attached can be accessed within the behavior through the yii\ base\ Behavior::owner property.

Static method binding behavior

For static binding behavior, you only need to overload yii\ base\ Component::behaviors (). This method is used to describe the behavior of the class. How do you describe it? Described using configuration, it can be the name of the Behavior class or the configuration array of the Behavior class:

Namespace app\ models;use yii\ db\ ActiveRecord;use app\ Components\ MyBehavior Class User extends ActiveRecord {public function behaviors () {return [/ / anonymous behavior, only give the class name of the behavior MyBehavior::className (), / / the behavior named myBehavior2, and also give the class name of the behavior 'myBehavior2' = > MyBehavior::className (), / / anonymous behavior The configuration array of MyBehavior class ['class' = > MyBehavior::className (),' prop1' = > 'value1',' prop3' = > 'value3',], / / the behavior named myBehavior4 is given. The configuration array of MyBehavior class is also given: 'myBehavior4' = > [' class' = > MyBehavior::className (), 'prop1' = >' value1', 'prop3' = >' value3',]] }}

Another static binding method is to bind through a configuration file:

['as myBehavior2' = > MyBehavior::className (),'as myBehavior3' = > ['class' = > MyBehavior::className (),' prop1' = > 'value1',' prop3' = > 'value3',],]

Dynamic method binding behavior

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 static binding behavior above.

In the above examples, the behavior is named after the key of the array, while the behavior that does not provide a key name is anonymous.

For the named behavior, you can call yii\ base\ Component::getBehavior () to get the bound behavior:

$behavior = $Component- > getBehavior ('myBehavior2')

For anonymous behavior, there is no way to quote it directly. However, you can get all the bound behaviors:

$behaviors = $Component- > getBehaviors ()

The internal principle of binding

Can you use the behavior so magically by simply overloading a yii\ base\ Component::behaviors ()? This is just the tip of the iceberg, which is actually related to the binding process, which is related to:

Yii\ base\ Component::behaviors () yii\ base\ Component::ensureBehaviors () yii\ base\ Component::attachBehaviorInternal () yii\ base\ Behavior::attach ()

Behavior is only one of the four methods, and more code is done in Component.

Yii\ base\ Component::behaviors () is already mentioned when it comes to static method binding behavior, which returns an array to describe the behavior. What about yii\ base\ Component::ensuerBehaviors ()?

This method will call _ _ get () _ set () _ isset () _ unset () _ call () canGetProperty () hasMethod () hasEventHandlers () on () off () in many places of Component. Is it a headache to see so much? It's not complicated at all, in a word, as long as it involves the properties, methods, and events of the class, this function will be called.

Who on earth is ensureBehaviors (), which is needed by so many mortals because of the stars and the moon? As the name suggests, his role is "ensure". It's just to make sure that the behavior described in behaviors () is bound:

Public function ensureBehaviors () {/ / means null is not bound / / one more sentence, empty array means no behavior if ($this- > _ behaviors = null) {$this- > _ behaviors = []; / / traverses the array returned by $this- > behaviors (), and binds foreach ($this- > behaviors () as $name = > $behavior) {$this- > attachBehaviorInternal ($name, $behavior);}}

This method is mainly used for subclasses, and yii\ base\ Compoent does not have any pre-injection behavior, so this call is useless. But for subclasses, you may overload yii\ base\ Compoent::behaviros () to pre-inject some behavior. Then, this function injects these behaviors first.

From the above code, you can naturally see the third thing to say next, yii\ base\ Component\ attachBehaviorInternal ():

Private function attachBehaviorInternal ($name, $behavior) {/ / is not a Behavior instance, saying it is just a class name and configuration array, so create if (! ($behavior instanceof Behavior)) {$behavior = Yii::createObject ($behavior);} / / anonymous behavior if (is_int ($name)) {$behavior- > attach ($this); $this- > _ behaviors [] = $behavior / / naming behavior} else {/ / already has a behavior with the same name. You need to release it first, and then bind the new behavior. If (isset ($this- > _ behaviors [$name])) {$this- > _ behaviors [$name]-> detach ();} $behavior- > attach ($this); $this- > _ behaviors [$name] = $behavior;} return $behavior;}

First of all, notice that this is a private member. In fact, in Yii, all methods with the suffix * Internal are private. This method does several things:

If the $behavior parameter is not a Behavior instance, it is created with Yii::createObject () as a parameter.

If you bind a behavior in the form of an anonymous behavior, attach the behavior directly to this class.

If it is a naming behavior, first see if any behavior with the same name has been bound to the class, and if so, replace the previous behavior with the later behavior.

In yii\ base\ Component::attachBehaviorInternal (), yii\ base\ Behavior::attach () is called with $this as the parameter. This leads to the last guy associated with binding, yii\ base\ Behavior::attach (), which we didn't finish when we talked about the elements of behavior earlier. Take a look at the code first:

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 manipulate the object on which it is attached

Traverses the array returned by events () in the behavior, binding the event that is ready to respond to the class through the on () of the attached class

Summary

Having said so much, let's make a summary about binding:

The binding action is initiated from Component

Static binding is achieved by overloading yii\ base\ Componet::behaviors ()

Dynamic binding is achieved by calling yii\ base\ Component::attachBehaviors ()

Behaviors can also be bound by configuring as configuration items for Component

Behavior can be divided into anonymous behavior and naming behavior, and the difference lies in whether the binding time is given a name. Naming behavior can be identified by its naming, so that operations such as release can be carried out pertinently.

During the binding process, the post-bound behavior replaces the bound behavior of the same name

The meaning of binding is twofold. One is to set $owner for the behavior. The second is to bind the handler of the event to be responded in the behavior to the class.

Thank you for reading, the above is the content of "the definition of behavior and binding method introduction in PHP Yii framework". After the study of this article, I believe you have a deeper understanding of the definition of behavior and binding method introduction in PHP Yii framework, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report