In the previous articles, we explained how to configure the behavior for a component class and how it works. In this article, we show how the Yii2 component accesses the properties of the behavior as if it were its own properties.

The first thing to say is that this isn’t complicated, but it should answer a lot of your previous questions about why you have to be a class that inherits components to use behavior. We all know that there is a magic method __get in PHP, and we need to look at it first.

First, explain the __get method

An object automatically calls the __get() method when accessing a nonexistent or unreachable member variable.

begin

It is through this method that yii2’s Component class accesses the properties of the associated behavior.

Look at the file vendor/yiisoft/yii2 / base/Component. PHP line127 __get method.

public function __get($name) {
    $getter = 'get' . $name;
    if (method_exists($this, $getter)) {
        // read property, e.g. getName()
        return $this->$getter();
    }

    // behavior property
    $this->ensureBehaviors();
    foreach ($this->_behaviors as $behavior) {
        if ($behavior->canGetProperty($name)) {
            return$behavior->$name; }}... }Copy the code

The method_exists($this, $getter) function first checks if method_exists($this, $getter) and then calls it if it does. Remember how you defined the associative method for AR.

Component performs its own $this->ensureBehaviors(); In the last article we learned that this function ensures that all related behavior objects are in place, and then iterates over all behavior objects.

If $behavior->canGetProperty($name) is true, return the associated property of the behavior (this property must be public), implementing the following result

$model = newUser(); $model->name; Left $model - > __get ('name'); left//$behavior->canGetProperty($name)
return $behavior->name;Copy the code

That’s the logic. Do you understand?

canGetProperty

The canGetProperty function is an object method. We know that Component and Behavior are subclasses of canGetProperty. This method is used to determine whether a property exists.

$behavior name = $behavior name = $behavior name = $behavior name = $behavior name = $behavior name = $behavior name = $behavior name = $behavior

namespace app\components;

use yii\base\Behavior;

class HelloBehavior extends Behavior {

    public function getName(a){
        return "abei20172"; }}// action
$model = new User();
echo $model->name;//abei20172Copy the code

Note that canGetProperty only checks whether the property exists and does not check whether the scope is public, private, or protected. Finally, of course, only public can access it correctly.

The following code

namespace app\components;

use yii\base\Behavior;

class HelloBehavior extends Behavior {
    protected $name = "abei2017";
}


// action
$model = new User();
echo $model->name;Copy the code

We’re going to get an exception.

Protected is not directly accessible

In this way

We use the __get method to inject behavior properties into component classes. Now you see why we can access them directly? Next we’ll talk about the injection principle of behavioral methods.