A class that is bound to behave like this:
Step 1: Define a class that will bind the behavior classes MyClass extends yii\base\component{ //null}//Step 2: Define a behavior class that will bind to MyClass class Mybehavio R extends a property of the yii\base\behavior{//Behavior public $property 1 = ' This is property in Mybehavior. '; A method of behavior public function method1 () { return ' Method in Mybehavior is called. '; }} $myClass = new MyClass (); $myBehavior = new Mybehavior ();//Step 3: Bind the behavior to the class $myclass->attachbehavior (' Mybehavior ', $ Mybehavior);//Step 4: Access the properties and methods of the behavior, just like the properties and methods of accessing the class itself echo $myClass->property1;echo $myClass->method1 ();
You don't have to understand the above code, although you may have guessed the meaning of the code with your toes, but here you just have to remember that the properties and methods in the behavior can be accessed directly by the class that is bound to it as if it were accessing its own properties and methods. In the code, $myClass is not a member of the Property1 method (). These two are members of the $myBehavior. However, after binding the behavior to the object through Attachbehavior (), $MYCALSS as if to become a star-sucking Dafa, the power Dafa, the performance of deep pockets, the attributes and methods of others have become their own.
In addition, from the code above, you also have to master the general process of using behavior:
- Derive your class from yii\base\component in order to use the behavior;
- Derive its own behavior class from Yii\base\behavior, which defines the properties and methods involved in the behavior;
- Bind component and behavior together;
- Like using the properties and methods of component itself, enjoy using the properties and methods defined in the behavior.
Using the behavior, you must first attach it to the Yii\base\component class or its subclasses, as described in the previous article. Once the behavior is attached to the component, it can be used directly.
After a behavior is attached to a component, you can access the properties defined by the public member variable or getter and setter methods of a behavior through the component:
"Prop1" is a property defined in the behavior class of Echo $component->prop1; $component->prop1 = $value; A public method that similarly can invoke the behavior://foo () is a public method that defines the behavior class in $ Component->foo ();
As you can see, although the $component does not define PROP1 and Foo (), they are used as a component of their own definition.
If all two behaviors define the same properties or methods, and they are all attached to the same component, the first behavior that is attached will have precedence when the property or method is accessed.
When attaching a behavior to a component's naming behavior, you can use this name to access the behavior object, as follows:
$behavior = $component->getbehavior (' Mybehavior ');
You can also get all the behaviors that are attached to this component:
$behaviors = $component->getbehaviors ();
Removal behavior
To remove the behavior, you can call Yii\base\component: the:d Etachbehavior () method is implemented with the name associated with the behavior:
$component->detachbehavior (' MyBehavior1 ');
You can also remove all behaviors:
$component->detachbehaviors ();
The above is introduced in the PHP Yii framework to use the behavior of behaviors, including the content, I hope the PHP tutorial interested in a friend helpful.