PHP中怎么實(shí)現(xiàn)static::與new static的靜態(tài)綁定?針對(duì)這個(gè)問題,今天小編總結(jié)這篇有關(guān)靜態(tài)綁定的文章,希望能幫助更多想解決這個(gè)問題的朋友找到更加簡(jiǎn)單易行的辦法。
從策劃到設(shè)計(jì)制作,每一步都追求做到細(xì)膩,制作可持續(xù)發(fā)展的企業(yè)網(wǎng)站。為客戶提供做網(wǎng)站、網(wǎng)站建設(shè)、網(wǎng)站策劃、網(wǎng)頁設(shè)計(jì)、域名注冊(cè)、虛擬空間、網(wǎng)絡(luò)營(yíng)銷、VI設(shè)計(jì)、 網(wǎng)站改版、漏洞修補(bǔ)等服務(wù)。為客戶提供更好的一站式互聯(lián)網(wǎng)解決方案,以客戶的口碑塑造優(yōu)易品牌,攜手廣大客戶,共同發(fā)展進(jìn)步。
一、parent、self、$this、__CLASS__
class A {} class B extends A{ parent::(public|protected)(靜態(tài)方法|靜態(tài)變量) ===> parent永遠(yuǎn)是A self::(public|protected)(靜態(tài)方法|靜態(tài)變量) ===> self永遠(yuǎn)是B $this->(public|protected)(非靜態(tài)方法|非靜態(tài)變量) ===> $this永遠(yuǎn)是B的是實(shí)例化對(duì)象 __CLASS__ ===> 永遠(yuǎn)是B } class C extends B{ parent::(public|protected)(靜態(tài)方法|靜態(tài)變量) ===> parent永遠(yuǎn)是B self::(public|protected)(靜態(tài)方法|靜態(tài)變量) ===> self永遠(yuǎn)是C $this->(public|protected)(非靜態(tài)方法|非靜態(tài)變量) ===> $this永遠(yuǎn)是C的是實(shí)例化對(duì)象 __CLASS__ ===> 永遠(yuǎn)是C }
二、static::
static關(guān)鍵字可以實(shí)現(xiàn)以下功能:
1 調(diào)用類的靜態(tài)方法 有后期靜態(tài)綁定效果;
2 調(diào)用類的靜態(tài)屬性 有后期靜態(tài)綁定效果;
3 調(diào)用類的非靜態(tài)方法 沒有后期靜態(tài)綁定效果;
4 注意:不可以調(diào)用非靜態(tài)屬性;
class A { private static function foo() { echo "A success!\n"; } public function test() { $this->foo(); } } class B extends A { } class C extends A { private static function foo() { echo "C success!\n"; } } $b = new B(); $b->test();//A success! $c = new C(); $c->test();//A success!
class A { private static function foo() { echo "A success!\n"; } public function test() { static::foo(); } } class B extends A { } class C extends A { private static function foo() { echo "C success!\n"; } } $b = new B(); $b->test();//A success! $c = new C(); $c->test();//A無法調(diào)用C里的私有foo方法 //將C的foo改成非private(public|protected)就可以解決 class A { private static function foo() { echo "A success!\n"; } public function test() { static::foo(); } } class B extends A { } class C extends A { public static function foo() { echo "C success!\n"; } } $b = new B(); $b->test();//A success! $c = new C(); $c->test();//C success!
class A { public static function foo() { static::who(); } public static function who() { echo __CLASS__."\n"; } } class B extends A { public static function test() { A::foo(); parent::foo(); self::foo(); } public static function who() { echo __CLASS__."\n"; } } class C extends B { public static function who() { echo __CLASS__."\n"; } } C::test(); A =>A::foo()的結(jié)果 C =>parent::foo()能走到A的foo,里面static::who找C::who C =>self::foo()能走到B的foo,B繼承A,走到A的foo,里面static::who找C::who class A { protected static function foo() { static::who(); } protected static function who() { echo __CLASS__."\n"; } } class B extends A { public static function test() { A::foo(); parent::foo(); self::foo(); } protected static function who() { echo __CLASS__."\n"; } } class C extends B { protected static function who() { echo __CLASS__."\n"; } } C::test(); //A C C,解釋同上 class A { public static function foo() { static::who(); } private static function who() { echo __CLASS__."\n"; } } class B extends A { public static function test() { A::foo(); parent::foo(); // self::foo(); } private static function who() { echo __CLASS__."\n"; } } class C extends B { private static function who() { echo __CLASS__."\n"; } } C::test(); //A =>A::foo()的結(jié)果 //報(bào)錯(cuò) A不可C的私有方法who => parent::foo()能走到A的foo,里面static::who找C::who,C的who只能在C里調(diào)用,不能在A里調(diào)用 //報(bào)錯(cuò) A不可C的私有方法who => self::foo()能走到B的foo,B繼承A,走到A的foo,里面static::who找C::who,C的who只能在C里調(diào)用,不能在A里調(diào)用
三、new static()
//new self()與new static()的區(qū)別,官網(wǎng)例子如下: class A { public static function get_self() { return new self(); } public static function get_static() { return new static(); } } class B extends A {} echo get_class(B::get_self()); // A echo get_class(B::get_static()); // B echo get_class(A::get_static()); // A
關(guān)于PHP中實(shí)現(xiàn)static::與new static的靜態(tài)綁定就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。