中文字幕日韩一区二区_国产一区二区av_国产毛片av_久久久久国产一区_色婷婷电影_国产一区二区精品

PHP中new static()與new self()的區別異同分析

本文實例講述了php中new static()與new self()的區別異同,相信對于大家學習php程序設計能夠帶來一定的幫助。

問題的起因是本地搭建一個站。發現用php 5.2 搭建不起來,站php代碼里面有很多5.3以上的部分,要求更改在5.2下能運行。

改著改著發現了一個地方

return new static($val);

這尼瑪是神馬,只見過

return new self($val);

于是上網查了下,他們兩個的區別。

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() ).

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()); // Aecho get_class(B::get_static()); // Becho get_class(A::get_static()); // A

這個例子基本上一看就懂了吧。

原理了解了,但是問題還沒有解決,如何解決掉 return new static($val); 這個問題呢?

其實也簡單就是用 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"*/

感興趣的朋友可以動手測試一下示例代碼,相信會有新的收獲!

php技術PHP中new static()與new self()的區別異同分析,轉載需保留來源!

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

主站蜘蛛池模板: 欧美日韩最新 | 国产一卡二卡三卡 | 午夜免费福利影院 | 九九热精品视频 | 欧美区日韩区 | 殴美黄色录像 | 久久午夜剧场 | www操操 | 91aiai| 国产网站在线免费观看 | 日本精品视频一区二区 | 天天操夜夜骑 | 日韩影院在线 | 九九热在线视频免费观看 | 久久的色 | 日韩最新网站 | 精品久久久久久红码专区 | 一区二区三区免费 | 91极品视频 | av手机在线播放 | 久久婷婷色 | 男女视频91 | 欧美黄色小视频 | 国产精品久久久久久久岛一牛影视 | 日韩三级电影在线看 | 亚洲欧美日本国产 | 北条麻妃av一区二区三区 | 久久久久久久久久久久久久久久久久久久 | 欧美成人a∨高清免费观看 色999日韩 | 黄色毛片在线观看 | 日韩精品一区二区三区在线观看 | 国产精品久久午夜夜伦鲁鲁 | 午夜小视频在线播放 | 欧美日韩视频在线 | 麻豆成人在线视频 | 欧美一级欧美一级在线播放 | 久久69精品久久久久久久电影好 | 亚洲一区欧美一区 | 天天操天天射综合网 | 波多野结衣先锋影音 | av电影一区二区 |