您好,登錄后才能下訂單哦!
PHP中怎么實現static::與new static的靜態綁定?針對這個問題,今天小編總結這篇有關靜態綁定的文章,希望能幫助更多想解決這個問題的朋友找到更加簡單易行的辦法。
一、parent、self、$this、__CLASS__
class A {} class B extends A{ parent::(public|protected)(靜態方法|靜態變量) ===> parent永遠是A self::(public|protected)(靜態方法|靜態變量) ===> self永遠是B $this->(public|protected)(非靜態方法|非靜態變量) ===> $this永遠是B的是實例化對象 __CLASS__ ===> 永遠是B } class C extends B{ parent::(public|protected)(靜態方法|靜態變量) ===> parent永遠是B self::(public|protected)(靜態方法|靜態變量) ===> self永遠是C $this->(public|protected)(非靜態方法|非靜態變量) ===> $this永遠是C的是實例化對象 __CLASS__ ===> 永遠是C }
二、static::
static關鍵字可以實現以下功能:
1 調用類的靜態方法 有后期靜態綁定效果;
2 調用類的靜態屬性 有后期靜態綁定效果;
3 調用類的非靜態方法 沒有后期靜態綁定效果;
4 注意:不可以調用非靜態屬性;
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無法調用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()的結果 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()的結果 //報錯 A不可C的私有方法who => parent::foo()能走到A的foo,里面static::who找C::who,C的who只能在C里調用,不能在A里調用 //報錯 A不可C的私有方法who => self::foo()能走到B的foo,B繼承A,走到A的foo,里面static::who找C::who,C的who只能在C里調用,不能在A里調用
三、new static()
//new self()與new static()的區別,官網例子如下: 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
關于PHP中實現static::與new static的靜態綁定就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。