我在看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)----youjiankuohaophpcnObject::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继承Object类的构建方法哪里调用Application的函数了
突然想起来了,Application extends Module extends ServiceLocator extends Component extends Object所以Application的构造函数里面的Component::construct根本就是在调用多层继承的父类的构造函数,所以可以理解Object中的$this最终还是指向Application实例。
立即学习“PHP免费学习笔记(深入)”;
public function construct($config = [])
{
Yii::$app = $this;
$this->setInstance($this);
$this->state = self::STATE_BEGIN;
$this->preInit($config);
$this->registerErrorHandler($config);
Component::construct($config);
}以上就是在php中构造函数里面调用无关类的构造函数有什么特殊含义的详细内容,更多请关注php中文网其它相关文章!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号