我在看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 调用Component的构建方法,Component继承的构建方法哪里调用Application的函数了 突然想起来了,Application 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);
} |