在建構函式裡面調用無關類的建構函式有什麼特殊含義

來源:互聯網
上載者:User
關鍵字 php yii2
我在看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基礎沒學好

回複內容:

我在看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);    }
  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.