【轉】PHP中的後期靜態繫結(Late Static Bindings )

來源:互聯網
上載者:User

標籤:public   pre   變數   code   提取   ruby   名稱   格式   echo   

php5.3版本新增的靜態繫結的靜態繫結文法,也成為PHP的後期靜態繫結,如下

class A{    public static function func1(){        echo __CLASS__.PHP_EOL;    }    public static function test(){        self::func1();    } }class B extend A{    public static function func1(){        echo __CLASS__.PHP_EOL;  //__CLASS__是當前類的名稱    }}B::test();結果:A

 

但是,既然B繼承了A,並重寫了A的func1,那麼如果想讓test中執行的是B中的func1要怎麼做呢?於是,php5.3使用了預留的static關鍵字,將上述代碼中的test函數改成這樣:

public static function test(){        static::func1();} 

結果:B

也就是說:static::實現了後期靜態繫結,static::對應的變數不再是當前類的變數,而是由最終運算時動態決定的(也可以說從堆裡面提取出來的關鍵字)。

同時,在這個學習的過程中,我還發現另外一個用法:new static() 和 new self(), 同樣的,new static 就是執行個體化最終繼承的類,而new self就是執行個體化當前類, new parent就是實現當前類的父類了。

好了,順便提一下形如這種格式的用法有三種: “self::”、”parent::”、”static::”;

【轉】PHP中的後期靜態繫結(Late Static Bindings )

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.