久久久久久久视色,久久电影免费精品,中文亚洲欧美乱码在线观看,在线免费播放AV片

<center id="vfaef"><input id="vfaef"><table id="vfaef"></table></input></center>

    <p id="vfaef"><kbd id="vfaef"></kbd></p>

    
    
    <pre id="vfaef"><u id="vfaef"></u></pre>

      <thead id="vfaef"><input id="vfaef"></input></thead>

    1. 站長資訊網(wǎng)
      最全最豐富的資訊網(wǎng)站

      詳解PHP中self關(guān)鍵字

      詳解PHP中self關(guān)鍵字

      解析PHP的self關(guān)鍵字

      PHP群里有人詢問self關(guān)鍵字的用法,答案是比較明顯的:

      靜態(tài)成員函數(shù)內(nèi)不能用this調(diào)用非成員函數(shù),但可以用self調(diào)用靜態(tài)成員函數(shù)/變量/常量;

      其他成員函數(shù)可以用self調(diào)用靜態(tài)成員函數(shù)以及非靜態(tài)成員函數(shù)。

      隨著討論的深入,發(fā)現(xiàn)self并沒有那么簡單。鑒于此,本文先對幾個關(guān)鍵字做對比和區(qū)分,再總結(jié)self的用法。

      要想將徹底搞懂self,要與parent、static、this區(qū)分開。

      以下分別做對比

      parent

      self與parent的區(qū)分比較容易:parent引用父類/基類被隱蓋的方法(或變量),self則引用自身方法(或變量)。

      例如構(gòu)造函數(shù)中調(diào)用父類構(gòu)造函數(shù):

      class Base {     public function __construct() {         echo "Base contructor!", PHP_EOL;     } }    class Child {     public function __construct() {         parent::__construct();         echo "Child contructor!", PHP_EOL;     } }    new Child; // 輸出: // Base contructor! // Child contructor!

      static

      static常規(guī)用途是修飾函數(shù)或變量使其成為類函數(shù)和類變量,也可以修飾函數(shù)內(nèi)變量延長其生命周期至整個應用程序的生命周期。

      但是其與self關(guān)聯(lián)上是PHP 5.3以來引入的新用途:靜態(tài)延遲綁定。

      有了static的靜態(tài)延遲綁定功能,可以在運行時動態(tài)確定歸屬的類。

      例如:

      class Base {     public function __construct() {         echo "Base constructor!", PHP_EOL;     }        public static function getSelf() {         return new self();     }        public static function getInstance() {         return new static();     }        public function selfFoo() {         return self::foo();     }        public function staticFoo() {         return static::foo();     }        public function thisFoo() {         return $this->foo();     }        public function foo() {         echo  "Base Foo!", PHP_EOL;     } }    class Child extends Base {     public function __construct() {         echo "Child constructor!", PHP_EOL;     }        public function foo() {         echo "Child Foo!", PHP_EOL;     } }    $base = Child::getSelf(); $child = Child::getInstance();    $child->selfFoo(); $child->staticFoo(); $child->thisFoo();

      程序輸出結(jié)果如下:

      Base constructor!
      Child constructor!
      Base Foo!
      Child Foo!
      Child Foo!

      在函數(shù)引用上,self與static的區(qū)別是:對于靜態(tài)成員函數(shù),self指向代碼當前類,static指向調(diào)用類;對于非靜態(tài)成員函數(shù),self抑制多態(tài),指向當前類的成員函數(shù),static等同于this,動態(tài)指向調(diào)用類的函數(shù)。

      parent、self、static三個關(guān)鍵字聯(lián)合在一起看挺有意思,分別指向父類、當前類、子類,有點“過去、現(xiàn)在、未來”的味道。

      this

      self與this是被討論最多,也是最容易引起誤用的組合。


      兩者的主要區(qū)別如下:

      this不能用在靜態(tài)成員函數(shù)中,self可以;

      對靜態(tài)成員函數(shù)/變量的訪問,建議 用self,不要用$this::或$this->的形式;

      對非靜態(tài)成員變量的訪問,不能用self,只能用this;

      this要在對象已經(jīng)實例化的情況下使用,self沒有此限制;

      在非靜態(tài)成員函數(shù)內(nèi)使用,self抑制多態(tài)行為,引用當前類的函數(shù);而this引用調(diào)用類的重寫(override)函數(shù)(如果有的話)。

      self的用途

      看完與上述三個關(guān)鍵字的區(qū)別,self的用途是不是呼之即出?一句話總結(jié),那就是:self總是指向“當前類(及類實例)”。

      詳細說則是:

      替代類名,引用當前類的靜態(tài)成員變量和靜態(tài)函數(shù);

      抑制多態(tài)行為,引用當前類的函數(shù)而非子類中覆蓋的實現(xiàn);


      槽點

      這幾個關(guān)鍵字中,只有this要加$符號且必須加,強迫癥表示很難受;

      靜態(tài)成員函數(shù)中不能通過$this->調(diào)用非靜態(tài)成員函數(shù),但是可以通過self::調(diào)用,且在調(diào)用函數(shù)中未使用$this->的情況下還能順暢運行。此行為貌似在不同PHP版本中表現(xiàn)不同,在當前的7.3中ok;

      在靜態(tài)函數(shù)和非靜態(tài)函數(shù)中輸出self,猜猜結(jié)果是什么?都是string(4) "self",迷之輸出;

      return $this instanceof static::class;會有語法錯誤,但是以下兩種寫法就正常: $class = static::class; return $this instanceof $class; // 或者這樣: return $this instanceof static;

      所以這是為什么???!

      $class = static::class;

      return $this instanceof $class;

      // 或者這樣:

      return $this instanceof static;

      推薦教程:《PHP視頻教程》

      贊(0)
      分享到: 更多 (0)
      網(wǎng)站地圖   滬ICP備18035694號-2    滬公網(wǎng)安備31011702889846號