您好,欢迎访问一九零五行业门户网

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

我在看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继承object类的构建方法哪里调用application的函数了
突然想起来了,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); }
以上就是在php中构造函数里面调用无关类的构造函数有什么特殊含义的详细内容。
其它类似信息

推荐信息