一 回顧trait使用 https://blog.csdn.net/bushuwei/article/details/103514174發現之前本人說明很模糊,自己居然不知道為什麼其實這裡的$c,就是class B再次回顧邏輯 二 分析 self和static區別說的沒毛病 Trait基類use t ...
一 回顧trait使用
https://blog.csdn.net/bushuwei/article/details/103514174
發現之前本人說明很模糊,自己居然不知道為什麼
其實這裡的$c,就是class B
再次回顧邏輯
二 分析
- self和static區別說的沒毛病
- Trait基類use trait,本類不use。那麼如果用的new self,則你new 出來的就是 use trait者。如果new static,則因為有繼承關係, 它會判斷類是否存在(父子會被認為都是同一個static,則不再new),那麼,誰先調用instance,那麼new出來的就是誰。‘Trait基類use trait,本類不use’->直接‘其實這裡的$c,就是class B’是錯的。之所以有這個‘幻覺’,是因為單例模式,且static
三 上代碼
- self+單例
trait A{ private static $instance; static function getInstance() { if(!isset(self::$instance)){ self::$instance = new self(); } return self::$instance; } } class B{ use A; function a() { var_dump('call at B'); } } class C extends B{ function a() { var_dump('call at c'); parent::a(); } } class D extends B{ use A; function a() { var_dump('call at D'); parent::a(); } } $b = B::getInstance(); $c = C::getInstance(); $d = D::getInstance(); $c->a(); echo "<br/>"; $d->a(); echo "<br/>";
string(9) "call at B" string(9) "call at D" string(9) "call at B"
註視掉
// $b = B::getInstance();
結果不變 - static+單例
trait A{ private static $instance; static function getInstance() { if(!isset(self::$instance)){ self::$instance = new static(); } return self::$instance; } } class B{ use A; function a() { var_dump('call at B'); } } class C extends B{ function a() { var_dump('call at c'); parent::a(); } } class D extends B{ use A; function a() { var_dump('call at D'); parent::a(); } } $b = B::getInstance(); $c = C::getInstance(); $d = D::getInstance(); $c->a(); echo "<br/>"; $d->a(); echo "<br/>";
string(9) "call at B" string(9) "call at D" string(9) "call at B"
註視掉
// $b = B::getInstance();
string(9) "call at c" string(9) "call at B" string(9) "call at D" string(9) "call at B"
-
self+非單例
trait A{ private static $instance; static function getInstance() { self::$instance = new self(); return self::$instance; } } class B{ use A; function a() { var_dump('call at B'); } } class C extends B{ function a() { var_dump('call at c'); parent::a(); } } class D extends B{ use A; function a() { var_dump('call at D'); parent::a(); } } $b = B::getInstance(); $c = C::getInstance(); $d = D::getInstance(); $c->a(); echo "<br/>"; $d->a(); echo "<br/>";
string(9) "call at B" string(9) "call at D" string(9) "call at B"
註釋掉
// $b = B::getInstance();
結果不變
- static+非單例
trait A{ private static $instance; static function getInstance() { // if(!isset(self::$instance)){ // self::$instance = new static(); // } // return self::$instance; self::$instance = new static(); return self::$instance; } } class B{ use A; function a() { var_dump('call at B'); } } class C extends B{ function a() { var_dump('call at c'); parent::a(); } } class D extends B{ use A; function a() { var_dump('call at D'); parent::a(); } } $b = B::getInstance(); $c = C::getInstance(); $d = D::getInstance(); $c->a(); echo "<br/>"; $d->a(); echo "<br/>";
string(9) "call at c" string(9) "call at B" string(9) "call at D" string(9) "call at B"
註釋掉
$b = B::getInstance();
結果不變
四 結論
- self的話,就是誰use的,就是誰
- static的話,誰調用的就是誰
- 但是static會出現‘誰use就是誰’的幻覺,那是因為單例模式,前面創建了相同類型的單例。