Home  >  Article  >  Backend Development  >  Detailed explanation of how to use behaviors in PHP's Yii framework

Detailed explanation of how to use behaviors in PHP's Yii framework

WBOY
WBOYOriginal
2016-07-29 08:57:33943browse

A class with bound behavior looks like this:

// Step 1: 定义一个将绑定行为的类
class MyClass extends yii\base\Component
{
  // 空的
}

// Step 2: 定义一个行为类,他将绑定到MyClass上
class MyBehavior extends yii\base\Behavior
{
  // 行为的一个属性
  public $property1 = 'This is property in MyBehavior.';

  // 行为的一个方法
  public function method1()
  {
    return 'Method in MyBehavior is called.';
  }
}

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

// Step 3: 将行为绑定到类上
$myClass->attachBehavior('myBehavior', $myBehavior);

// Step 4: 访问行为中的属性和方法,就和访问类自身的属性和方法一样
echo $myClass->property1;
echo $myClass->method1();

You don’t have to understand all the above codes, although you may have guessed the meaning of these codes with your toes, but here you just need to remember The properties and methods in the behavior can be accessed directly by the bound class just like accessing its own properties and methods. In the code, $myClass does not have a property1 method() member. These two are members of $myBehavior. However, after binding the behavior to the object through attachBehavior(), $myCalss seems to have mastered the magic of attracting stars and transforming energy, showing great wealth and turning other people's attributes and methods into its own.

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

  • Derive your own class from yiibaseComponent in order to use the behavior;
  • Derive your own behavior class from yiibaseBehavior, which defines the behavior involved Properties and methods;
  • Bind Component and Behavior;
  • Use the properties and methods defined in the behavior just like using the properties and methods of the Component itself.

To use a behavior, you must first attach it to the yiibaseComponent class or its subclass as described previously. Once a behavior is attached to a component, you can use it directly.

After a behavior is attached to a component, you can access a behavior's public member variables or properties defined by getter and setter methods through the component:

// "prop1" 是定义在行为类的属性
echo $component->prop1;
$component->prop1 = $value;
类似地也可以调用行为的公共方法:

// foo() 是定义在行为类的公共方法
$component->foo();

As you can see, although $component does not define prop1 and foo(), they are used It looks like the component itself defines it.

If two behaviors define the same property or method, and they are both attached to the same component, then the behavior attached first will have priority when the property or method is accessed.

The named behavior when attaching a behavior to a component, you can use this name to access the behavior object, as shown below:

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

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

$behaviors = $component->getBehaviors();

Remove behavior

To remove a behavior, you can call the yiibaseComponent::detachBehavior() method using the name associated with the behavior:

$component->detachBehavior('myBehavior1');

You can also remove all behaviors:

$component->detachBehaviors();

The above has introduced in detail the method of using Behaviors in the Yii framework of PHP, including all aspects. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn