PHP中new static()與new self()的區(qū)別異同分析
本文實(shí)例講述了PHP中new static()與new self()的區(qū)別異同,相信對(duì)于大家學(xué)習(xí)PHP程序設(shè)計(jì)能夠帶來(lái)一定的幫助。
問(wèn)題的起因是本地搭建一個(gè)站。發(fā)現(xiàn)用PHP 5.2 搭建不起來(lái),站PHP代碼里面有很多5.3以上的部分,要求更改在5.2下能運(yùn)行。
改著改著發(fā)現(xiàn)了一個(gè)地方
return new static($val);
這尼瑪是神馬,只見(jiàn)過(guò)
return new self($val);
于是上網(wǎng)查了下,他們兩個(gè)的區(qū)別。
self - 就是這個(gè)類(lèi),是代碼段里面的這個(gè)類(lèi)。
static - PHP 5.3加進(jìn)來(lái)的只得是當(dāng)前這個(gè)類(lèi),有點(diǎn)像$this的意思,從堆內(nèi)存中提取出來(lái),訪問(wèn)的是當(dāng)前實(shí)例化的那個(gè)類(lèi),那么 static 代表的就是那個(gè)類(lèi)。
還是看看老外的專業(yè)解釋吧:
self refers to the same class whose method the new operation takes place in.
static in PHP 5.3's late static bindings refers to whatever class in the hierarchy which you call the method on.
In the following example, B inherits both methods from A. self is bound to A because it's defined in A's implementation of the first method, whereas static is bound to the called class (also see get_called_class() ).
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
這個(gè)例子基本上一看就懂了吧。
原理了解了,但是問(wèn)題還沒(méi)有解決,如何解決掉 return new static($val); 這個(gè)問(wèn)題呢?
其實(shí)也簡(jiǎn)單就是用 get_class($this); 代碼如下:
class A {
public function create1() {
$class = get_class($this);
return new $class();
}
public function create2() {
return new static();
}
}
class B extends A {
}
$b = new B();
var_dump(get_class($b->create1()), get_class($b->create2()));
/*
The result
string(1) "B"
string(1) "B"
*/
感興趣的朋友可以動(dòng)手測(cè)試一下示例代碼,相信會(huì)有新的收獲!
- php self,$this,const,static,->的使用
- php類(lèi)中的$this,static,final,const,self這幾個(gè)關(guān)鍵字使用方法
- PHP中new static() 和 new self() 的區(qū)別介紹
- PHP中static關(guān)鍵字以及與self關(guān)鍵字的區(qū)別
- PHP new static 和 new self詳解
- PHP面向?qū)ο笾衝ew self()與 new static()的區(qū)別淺析
- 淺談PHP中new self()和new static()的區(qū)別
- php類(lèi)中static與self的使用區(qū)別淺析
相關(guān)文章
PHP實(shí)現(xiàn)蛇形矩陣,回環(huán)矩陣及數(shù)字螺旋矩陣的方法分析
這篇文章主要介紹了PHP實(shí)現(xiàn)蛇形矩陣,回環(huán)矩陣及數(shù)字螺旋矩陣的方法,結(jié)合具體實(shí)例形式分析了蛇形矩陣,回環(huán)矩陣及數(shù)字螺旋矩陣的概念、表示方法及php實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-05-05
gearman管理工具GearmanManager的安裝與php使用方法示例
這篇文章主要介紹了gearman管理工具GearmanManager的安裝與php使用方法,結(jié)合實(shí)例形式詳細(xì)分析了gearman管理工具GearmanManager的安裝及php使用GearmanManager相關(guān)配置與操作注意事項(xiàng),需要的朋友可以參考下2020-02-02
Thinkphp框架開(kāi)發(fā)移動(dòng)端接口(2)
這篇文章主要介紹了thinkphp框架開(kāi)發(fā)移動(dòng)端接口的第2種方法,實(shí)現(xiàn)移動(dòng)端訪問(wèn)自動(dòng)切換移動(dòng)主題模板,從而實(shí)現(xiàn)偽app訪問(wèn),感興趣的小伙伴們可以參考一下2016-08-08
PHP mongodb操作類(lèi)定義與用法示例【適合mongodb2.x和mongodb3.x】
這篇文章主要介紹了PHP mongodb操作類(lèi)定義與用法,結(jié)合實(shí)例形式分析了php封裝的適合mongodb2.x和mongodb3.x版本MongoDB數(shù)據(jù)庫(kù)連接、增刪改查、錯(cuò)誤處理等操作定義與使用方法,需要的朋友可以參考下2018-06-06
解析WordPress中函數(shù)鉤子hook的作用及基本用法
這篇文章主要介紹了解析WordPress中函數(shù)鉤子hook的作用及基本用法,hook是WordPress中調(diào)用函數(shù)的重要用法,也是插件開(kāi)發(fā)的基礎(chǔ),需要的朋友可以參考下2015-12-12
php版微信數(shù)據(jù)統(tǒng)計(jì)接口用法示例
這篇文章主要介紹了php版微信數(shù)據(jù)統(tǒng)計(jì)接口用法,結(jié)合實(shí)例形式分析了php微信數(shù)據(jù)統(tǒng)計(jì)接口功能及相關(guān)的使用技巧,需要的朋友可以參考下2016-10-10
php實(shí)現(xiàn)轉(zhuǎn)換ubb代碼的方法
這篇文章主要介紹了php實(shí)現(xiàn)轉(zhuǎn)換ubb代碼的方法,涉及php正則替換的使用技巧,需要的朋友可以參考下2015-06-06

