Detailed description of the Behaviors method used in the PHP Yii Framework, yiibehaviors_PHP tutorial

Source: Internet
Author: User
Explain how to use Behaviors in the Yii Framework of PHP. In the Yii Framework of PHP, the behavior Behaviors method is used in detail. a yiibehaviors class bound with the behavior is shown as follows: Step 1: defines a class classM that will bind behavior detailed explanation in the PHP Yii Framework using the behavior Behaviors method, yiibehaviors

A behavior-bound class is shown as follows:

// Step 1: define a class for binding behaviors. class MyClass extends yii \ base \ Component {// Null} // Step 2: define an action class, he will bind the class MyBehavior extends yii \ base \ Behavior {// an attribute of the Behavior public $ property1 = 'This is property in MyBehavior. '; // A behavior Method 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 ('myhavior ', $ myBehavior); // Step 4: Attributes and methods in the access behavior, echo $ myClass-> property1; echo $ myClass-> method1 ();

You don't need to understand all the above code. although you may have guessed the meaning of the code with your toes, but here you only need to remember that the attributes and methods in the behavior can be directly accessed by the bound class just like accessing its own attributes and methods. In the code, $ myClass does not have a property1 method () member. These two are members of $ myBehavior. However, after attachBehavior () is used to bind the behavior to an object, $ myCalss seems to have become a method of absorbing stars, making great achievements, and showing great wealth, turn others' attributes and methods into their own.

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

  • Derive your own class from yii \ base \ Component for use;
  • Derive your own Behavior class from yii \ base \ Behavior, which defines the attributes and methods involved in the Behavior;
  • Bind Component and Behavior;
  • Just like using Component's own attributes and methods, you can use the attributes and methods defined in behavior.

The usage behavior must be appended to the yii \ base \ Component class or its subclass as described above. Once the behavior is attached to the component, you can directly use it.

After the behavior is appended to the component, you can access the public member variable of an action or the attributes defined by the getter and setter methods through the component:

// "Prop1" is the property defined in the behavior class echo $ component-> prop1; $ component-> prop1 = $ value; similarly, you can call public methods of behavior: // foo () is the common method defined in the behavior class $ component-> foo ();

As you can see, although $ component does not define prop1 and foo (), they are used as defined by the component itself.

If both actions define the same attributes or methods and they are appended to the same component, the first action to be appended has a priority when the attribute or method is accessed.

You can use this name to access behavior objects when appending behavior to a component, as shown below:

$behavior = $component->getBehavior('myBehavior');

You can also obtain all the actions attached to this component:

$behaviors = $component->getBehaviors();

Removal behavior

To remove a behavior, you can call the yii \ base \ Component: detachBehavior () method to implement it with the name associated with the behavior:

$component->detachBehavior('myBehavior1');

You can also remove all actions:

$component->detachBehaviors();

Articles you may be interested in:
  • How to remove the behavior bound to a component from the Yii Framework of PHP
  • Definition and binding of behavior 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.

Define a class bound to the behavior, as shown in the following figure: // Step 1: define a class M that will bind the behavior...

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.