Explain how to use behavioral behaviors in the YII framework of PHP _php tips

Source: Internet
Author: User

A class that is bound to behave like this:

Step 1: Define a Classes Class
MyClass extends yii\base\component
{
  //Empty
}

/Step 2: Define a Behavior class, He will bind to the MyClass
class Mybehavior extends Yii\base\behavior
{
  //behavior of an attribute public
  $property 1 = ' the ' is Property in Mybehavior. ';

  A method of the behavior public
  function method1 () {return ' methods in
  Mybehavior is
    called. '
  }
}

$myClass = new MyClass ();
$myBehavior = new Mybehavior ();

Step 3: Bind the behavior to the class
$myClass->attachbehavior (' Mybehavior ', $myBehavior);

Step 4: The properties and methods in the access behavior, as well as the properties and methods of accessing the class itself
echo $myClass->property1;
echo $myClass->method1 ();

You don't have to understand the code above, 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 of the behavior can be accessed directly by the class like the property and method that you are bound to. In 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 seems to have become the star-sucking Dafa, the power Dafa, the expressive deep pockets, the other people's attributes and methods into their own.

In addition, from the above code, you have to master the general process of using behavior:

    • Derive your own class from the yii\base\component to use the behavior;
    • Derive your own behavioral class from Yii\base\behavior, which defines the attributes and methods involved in the behavior;
    • Bind component and behavior;
    • Like using component's own properties and methods, use the properties and methods defined in the behavior.

Use behavior, you must 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, you can use it directly.

After the behavior is attached to the 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 defined in the properties of the behavior class
echo $component->prop1;
$component->prop1 = $value;
A public method that can similarly invoke the behavior:

//foo () is a public method defined in the behavior class
$component->foo ();

As you can see, although $component do not define PROP1 and Foo (), they are also used as defined by the components themselves.

If all two behaviors define the same properties or methods, and they are all attached to the same component, then the behavior appended first has precedence when the property or method is accessed.

The naming behavior when attaching behavior to a component can be used to access the behavior object as follows:

$behavior = $component->getbehavior (' Mybehavior ');

You can also get all the behaviors attached to this component:

$behaviors = $component->getbehaviors ();

Removal behavior

To remove the behavior, you can call Yii\base\component::d Etachbehavior () method is implemented with the name associated with the behavior:

$component->detachbehavior (' MyBehavior1 ');

You can also remove all behaviors:

$component->detachbehaviors ();

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.