本文實(shí)例講述了PHP面向?qū)ο蟪绦蛟O(shè)計(jì)中的self、static、parent關(guān)鍵字用法.分享給大家供大家參考,具體如下:
看到php里面有關(guān)于后期靜態(tài)綁定的內(nèi)容,雖然沒有完全看懂,但是也收獲不少東西。
php官方手冊(cè)介紹:
http://php.net/manual/zh/language.oop5.late-static-bindings.php
不存在繼承的時(shí)候
不存在繼承的意思就是,就書寫一個(gè)單獨(dú)的類來(lái)使用的時(shí)候。self和static在范圍解析操作符 (::) 的使用上,并無(wú)區(qū)別。
此時(shí),self和static的表現(xiàn)是一樣的,可以替換為該類名::的方式調(diào)用。
<?phpclass Demo{ public static $static; public $Nostatic; public function __construct(){ self::$static = "static"; $this->Nostatic = "Nostatic"; } public static function get(){ return __CLASS__; } public function show(){ return "this is function show with ".$this->Nostatic; } public function test(){ echo Demo::$static."<br/>"; //使用類名調(diào)用靜態(tài)屬性 echo Demo::get()."<br/>"; //使用類名調(diào)用靜態(tài)屬性 echo Demo::show()."<br/>"; //使用類名調(diào)用靜態(tài)屬性 echo self::$static."<br/>"; //self調(diào)用靜態(tài)屬性 echo self::show()."<br/>"; //self調(diào)用非靜態(tài)方法 echo self::get()."<br/>"; //self調(diào)用靜態(tài)方法 echo static::$static."<br/>";//static調(diào)用靜態(tài)屬性 echo static::show()."<br/>";//static調(diào)用非靜態(tài)方法 echo static::get()."<br/>"; //static調(diào)用靜態(tài)方法 }}$obj = new Demo();$obj->test();
輸出結(jié)果:
static
Demo
this is function show with Nostatic
static
this is function show with Nostatic
Demo
static
this is function show with Nostatic
Demo
繼承的時(shí)候
在繼承時(shí),self和static在范圍解析操作符 (::) 的使用上有差別。parent也是在繼承的時(shí)候使用的。
<?phpclass A{ static function getClassName(){ return "this is class A"; } static function testSelf(){ echo self::getClassName(); } static function testStatic(){ echo static::getClassName(); }}class B extends A{ static function getClassName(){ return "this is class B"; }}B::testSelf();echo "<br/>";B::testStatic();
輸出結(jié)果:
this is class A
this is class B
self調(diào)用的靜態(tài)方法或?qū)傩允冀K表示其在使用的時(shí)候的當(dāng)前類(A)的方法或?qū)傩裕梢蕴鎿Q為其類名,但是在類名很長(zhǎng)或者有可能變化的情況下,使用self::的方式無(wú)疑是更好的選擇。
static調(diào)用的靜態(tài)方法或?qū)傩詴?huì)在繼承中被其子類重寫覆蓋,應(yīng)該替換為對(duì)應(yīng)的子類名(B)。
parent關(guān)鍵字用于調(diào)用父類的方法和屬性。在靜態(tài)方法中,可以調(diào)用父類的靜態(tài)方法和屬性;在非靜態(tài)方法中,可以調(diào)用父類的方法和屬性。
<?phpclass A{ public static $static; public $Nostatic; public function __construct(){ self::$static = "static"; $this->Nostatic = "Nostatic"; } public static function staticFun(){ return self::$static; } public function noStaticFun(){ return "this is function show with ".$this->Nostatic; }}class B extends A{ static function testS(){ echo parent::staticFun(); } function testNoS(){ echo parent::noStaticFun(); }}$obj = new B();$obj->testS();echo "<br/>";$obj->testNoS();
輸出結(jié)果
static
this is function show with Nostatic
在文章的最后,我們分析一個(gè)手冊(cè)上的例子
<?phpclass 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();?>
輸出結(jié)果
A
C
C
我們單獨(dú)拿出test方法進(jìn)行分析:
public static function test() { A::foo(); parent::foo(); self::foo();}
1)A::foo();
這個(gè)語(yǔ)句是可以在任何地方執(zhí)行的,它表示使用A去調(diào)用靜態(tài)方法foo()得到'A'。
2)parent::foo();
C的parent是B,B的parent是A,回溯找到了A的foo方法;static::who();
語(yǔ)句中的static::調(diào)用的方法會(huì)被子類覆蓋,所以優(yōu)先調(diào)用C的who()方法,如果C的who方法不存在會(huì)調(diào)用B的who方法,如果B的who方法不存在會(huì)調(diào)用A的who方法。所以,輸出結(jié)果是'C'。[注1]
3)self::foo();
這個(gè)self::是在B中使用的,所以self::等價(jià)于B::,但是B沒有實(shí)現(xiàn)foo方法,B又繼承自A,所以我們實(shí)際上調(diào)用了A::foo()
這個(gè)方法。foo方法使用了static::who()
語(yǔ)句,導(dǎo)致我們又調(diào)用了C的who函數(shù)。[注2]
注1:補(bǔ)充解釋上面的(2)
<?phpclass 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();?>
輸出結(jié)果:
A B B
注2:補(bǔ)充解釋上面的(3)
<?phpclass 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 foo() { echo "fooB"."/n"; } public static function who() { echo __CLASS__."/n"; }}class C extends B { public static function foo() { echo "fooC"."/n"; } public static function who() { echo __CLASS__."/n"; }}C::test();?>
輸出結(jié)果:
A C fooB
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
新聞熱點(diǎn)
疑難解答
圖片精選