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