Commit e8b716cf by Larry Ullman

Edited attaching behaviors

parent d5a79df6
...@@ -100,11 +100,11 @@ function ($event) { ...@@ -100,11 +100,11 @@ function ($event) {
Attaching Behaviors <a name="attaching-behaviors"></a> Attaching Behaviors <a name="attaching-behaviors"></a>
------------------- -------------------
You can attach a behavior to a [[yii\base\Component|component]] either statically or dynamically. The former You can attach a behavior to a [[yii\base\Component|component]] either statically or dynamically. The former is more common in practice.
is more commonly used in practice.
To attach a behavior statically, override the [[yii\base\Component::behaviors()|behaviors()]] method of the component To attach a behavior statically, override the [[yii\base\Component::behaviors()|behaviors()]] method of the component
class that it is being attached. For example, class to which the behavior is being attached. The [[yii\base\Component::behaviors()|behaviors()]] method should return a list of behavior [configurations](concept-configurations.md).
Each behavior configuration can be either a behavior class name or a configuration array:
```php ```php
namespace app\models; namespace app\models;
...@@ -141,16 +141,11 @@ class User extends ActiveRecord ...@@ -141,16 +141,11 @@ class User extends ActiveRecord
} }
``` ```
The [[yii\base\Component::behaviors()|behaviors()]] method should return a list of behavior [configurations](concept-configurations.md). You may associate a name with a behavior by specifying the array key corresponding to the behavior configuration. In this case, the behavior is called a *named behavior*. In the above example, there are two named behaviors:
Each behavior configuration can be either a behavior class name or a configuration array.
You may associate a name with a behavior by specifying the array key corresponding to the behavior configuration.
In this case, the behavior is called a *named behavior*. In the above example, there are two named behaviors:
`myBehavior2` and `myBehavior4`. If a behavior is not associated with a name, it is called an *anonymous behavior*. `myBehavior2` and `myBehavior4`. If a behavior is not associated with a name, it is called an *anonymous behavior*.
To attach a behavior dynamically, call the [[yii\base\Component::attachBehavior()]] method of the component To attach a behavior dynamically, call the [[yii\base\Component::attachBehavior()]] method of the component to which the behavior is being attached:
that it is attached to. For example,
```php ```php
use app\components\MyBehavior; use app\components\MyBehavior;
...@@ -169,8 +164,7 @@ $component->attachBehavior('myBehavior3', [ ...@@ -169,8 +164,7 @@ $component->attachBehavior('myBehavior3', [
]); ]);
``` ```
You may attach multiple behaviors at once by using the [[yii\base\Component::attachBehaviors()]] method. You may attach multiple behaviors at once using the [[yii\base\Component::attachBehaviors()]] method:
For example,
```php ```php
$component->attachBehaviors([ $component->attachBehaviors([
...@@ -179,8 +173,7 @@ $component->attachBehaviors([ ...@@ -179,8 +173,7 @@ $component->attachBehaviors([
]); ]);
``` ```
You may also attach behaviors through [configurations](concept-configurations.md) like the following. For more details, You may also attach behaviors through [configurations](concept-configurations.md) like the following:
please refer to the [Configurations](concept-configurations.md#configuration-format) section.
```php ```php
[ [
...@@ -194,6 +187,9 @@ please refer to the [Configurations](concept-configurations.md#configuration-for ...@@ -194,6 +187,9 @@ please refer to the [Configurations](concept-configurations.md#configuration-for
] ]
``` ```
For more details,
please refer to the [Configurations](concept-configurations.md#configuration-format) section.
Using Behaviors <a name="using-behaviors"></a> Using Behaviors <a name="using-behaviors"></a>
--------------- ---------------
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment