Home > Backend Development > PHP Tutorial > What is the special meaning of calling the constructor of an unrelated class in the constructor of PHP?

What is the special meaning of calling the constructor of an unrelated class in the constructor of PHP?

黄舟
Release: 2023-03-12 10:24:01
Original
1052 people have browsed it

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);
    }
Copy after login

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;
    }
Copy after login

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);
    }
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template