我在看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); }