php new static 效率,PHP中new static()与new self()的比较
今天在coding的時(shí)候,發(fā)現(xiàn)了 new static(),覺得實(shí)例化的地方不是應(yīng)該是 new self()嗎?查詢了一下才知道兩者的區(qū)別:
1)在有子類集成的時(shí)候,兩者的表現(xiàn)不一樣
2)php 5.2及以下的版本不支持 new static()的語法
簡(jiǎn)單通俗的來說, self就是寫在哪個(gè)類里面, 實(shí)際調(diào)用的就是這個(gè)類.所謂的后期靜態(tài)綁定, static代表使用的這個(gè)類, 就是你在父類里寫的static,
然后通過子類直接/間接用到了這個(gè)static, 這個(gè)static指的就是這個(gè)子類, 所以說static和$this很像, 但是static可以用于靜態(tài)方法和屬性等.
具體解釋如下:
self - 就是這個(gè)類,是代碼段里面的這個(gè)類。
static - PHP 5.3加進(jìn)來的只得是當(dāng)前這個(gè)類,有點(diǎn)像$this的意思,從堆內(nèi)存中提取出來,訪問的是當(dāng)前實(shí)例化的那個(gè)類,那么 static 代表的就是那個(gè)類。
還是看看老外的專業(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() ).
上代碼:
但是如果想讓 子類使用 get_class時(shí),返回的也是 當(dāng)前子類的名稱('wangbaoqiang'),該怎么做呢。
以上所述是小編給大家介紹的PHP中new static()與new self()的比較,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)服務(wù)器之家網(wǎng)站的支持!
原文鏈接:http://www.cnblogs.com/aiweixiao/archive/2016/08/19/5786973.html
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的php new static 效率,PHP中new static()与new self()的比较的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: windows。forms.timer设
- 下一篇: apt-get install php5