I am watching the startup process of Yii2, in which the parent class of Application constructor is written like this; I would like to ask what is special about the last sentence Component::construct($config) being called here Meaning?
public function construct($config = []) { Yii::$app = $this; $this->setInstance($this); $this->state = self::STATE_BEGIN; $this->preInit($config); $this->registerErrorHandler($config); Component::construct($config); }
Mainly because there is a problem that I cannot understand in the process of tracking the code,
Component::construct($config)---->Object::construct($config)---> ;Yii::configure($this,$config)
This is how it is handled in Yii::configure:
public static function configure($object, $properties) { foreach ($properties as $name => $value) { $object->$name = $value; } return $object; }
The corresponding setter function is actually called here, and $config generally contains components, so the setComponents function will be called. This function is defined by the Application parent class, so why can the $this instance call the Application function? $this is obviously an instance of Component, so I can’t understand it. I haven’t learned the basics of PHP well.
application calls the construction method of Component. The construction method of Component inheritsObject classwhere does it call the function of Application
I suddenly remembered that Application extends Module extends ServiceLocator extends Component extends Object, so Component::construct in the constructor of Application is basically calling the constructor of the parent class with multi-layer inheritance. So it can be understood that $this in Object ultimately points to the Application instance.
public function construct($config = []) { Yii::$app = $this; $this->setInstance($this); $this->state = self::STATE_BEGIN; $this->preInit($config); $this->registerErrorHandler($config); Component::construct($config); }
The above is the detailed content of What is the special meaning of calling the constructor of an unrelated class in the constructor of PHP?. For more information, please follow other related articles on the PHP Chinese website!