亚洲最大看欧美片,亚洲图揄拍自拍另类图片,欧美精品v国产精品v呦,日本在线精品视频免费

  • 站長(zhǎng)資訊網(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并沒有那么簡(jiǎn)單。鑒于此,本文先對(duì)幾個(gè)關(guān)鍵字做對(duì)比和區(qū)分,再總結(jié)self的用法。

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

    以下分別做對(duì)比

    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)變量延長(zhǎng)其生命周期至整個(gè)應(yīng)用程序的生命周期。

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

    有了static的靜態(tài)延遲綁定功能,可以在運(yùn)行時(shí)動(dòng)態(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ū)別是:對(duì)于靜態(tài)成員函數(shù),self指向代碼當(dāng)前類,static指向調(diào)用類;對(duì)于非靜態(tài)成員函數(shù),self抑制多態(tài),指向當(dāng)前類的成員函數(shù),static等同于this,動(dòng)態(tài)指向調(diào)用類的函數(shù)。

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

    this

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


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

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

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

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

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

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

    self的用途

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

    詳細(xì)說則是:

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

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


    槽點(diǎn)

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

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

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

    return $this instanceof static::class;會(huì)有語(yǔ)法錯(cuò)誤,但是以下兩種寫法就正常: $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號(hào)-2    滬公網(wǎng)安備31011702889846號(hào)