首页 > 在构造函数里面调用无关类的构造函数有什么特殊含义

在构造函数里面调用无关类的构造函数有什么特殊含义

我在看Yii2的启动过程,其中Application的父类构造函数是这么写的;
我想问一下最后一句Component::__construct($config)在这里调用有什么特殊的含义吗?


    public function __construct($config = [])
    {
        Yii::$app = $this;
        $this->setInstance($this);

        $this->state = self::STATE_BEGIN;

        $this->preInit($config);

        $this->registerErrorHandler($config);

        Component::__construct($config);
    }

主要是在跟踪代码的过程中有一个问题无法理解,
Component::__construct($config)---->Object::__construct($config)--->Yii::configure($this,$config)
在Yii::configure里面是这么处理的:

  public static function configure($object, $properties)
    {
        foreach ($properties as $name => $value) {
            $object->$name = $value;
        }

        return $object;
    }

这里实际上最终要调用到相应的setter函数,其中$config一般包含components的设置,所以会调用到setComponents函数,这个函数时Application父类定义的,所以这里$this实例为什么可以调用到Application的函数?
$this明明是Component的实例嘛,所以理解不了,PHP基础没学好


突然想起来了,Application extends Module extends ServiceLocator extends Component extends Object
所以Application的构造函数里面的Component::__construct根本就是在调用多层继承的父类的构造函数,所以可以理解Object中的$this最终还是指向Application实例。

  public function __construct($config = [])
    {
        Yii::$app = $this;
        $this->setInstance($this);

        $this->state = self::STATE_BEGIN;

        $this->preInit($config);

        $this->registerErrorHandler($config);

        Component::__construct($config);
    }

application 调用Component的构建方法,Component继承Object类的构建方法哪里调用Application的函数了

【热门文章】
【热门文章】