首頁 > php教程 > php手册 > PHP中new static()與new self()的比較

PHP中new static()與new self()的比較

WBOY
發布: 2016-08-20 08:47:38
原創
854 人瀏覽過

  今天在coding的時候,發現了 new static(),覺得實例化的地方不是應該是 new self()嗎?查詢了一下才知道兩者的差別:

  1)在有子類別整合的時候,兩者的表現不一樣

  2)php 5.2及以下的版本不支援 new 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() ).
登入後複製

 

上代碼:

 

<span style="color: #008080;"> 1</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> Person {
</span><span style="color: #008080;"> 2</span>   <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> get_self() {
</span><span style="color: #008080;"> 3</span>     <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">new</span><span style="color: #000000;"> self();
</span><span style="color: #008080;"> 4</span> <span style="color: #000000;">  }
</span><span style="color: #008080;"> 5</span>  
<span style="color: #008080;"> 6</span>   <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> get_static() {
</span><span style="color: #008080;"> 7</span>     <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">new</span> <span style="color: #0000ff;">static</span><span style="color: #000000;">();
</span><span style="color: #008080;"> 8</span> <span style="color: #000000;">  }
</span><span style="color: #008080;"> 9</span> <span style="color: #000000;">}
</span><span style="color: #008080;">10</span>  
<span style="color: #008080;">11</span> <span style="color: #0000ff;">class</span> WangBaoqiang <span style="color: #0000ff;">extends</span><span style="color: #000000;"> Person{}
</span><span style="color: #008080;">12</span>  
<span style="color: #008080;">13</span> <span style="color: #0000ff;">echo</span> <span style="color: #008080;">get_class</span>(WangBaoqiang::get_self()); <span style="color: #008000;">//</span><span style="color: #008000;"> Person</span>
<span style="color: #008080;">14</span> <span style="color: #0000ff;">echo</span> <span style="color: #008080;">get_class</span>(WangBaoqiang::get_static()); <span style="color: #008000;">//</span><span style="color: #008000;"> WangBaoqiang</span>
<span style="color: #008080;">15</span> <span style="color: #0000ff;">echo</span> <span style="color: #008080;">get_class</span>(Person::get_static()); <span style="color: #008000;">//</span><span style="color: #008000;"> Person</span>
登入後複製

 

但是如果想讓 子類別使用 get_class時,傳回的也是 目前子類別的名稱('wangbaoqiang'),該怎麼做呢。

<?<span style="color: #000000;">php
</span><span style="color: #0000ff;">class</span><span style="color: #000000;"> Person {
  </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> create1() {
    </span><span style="color: #800080;">$class</span> = <span style="color: #008080;">get_class</span>(<span style="color: #800080;">$this</span><span style="color: #000000;">);
    </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">new</span> <span style="color: #800080;">$class</span><span style="color: #000000;">();
  }
  </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> create2() {
    </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">new</span> <span style="color: #0000ff;">static</span><span style="color: #000000;">();
  }
}
 
</span><span style="color: #0000ff;">class</span> WangBaoqiang <span style="color: #0000ff;">extends</span><span style="color: #000000;"> Person {
 
}
 
</span><span style="color: #800080;">$wangBaoQiang</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> WangBaoqiang();
</span><span style="color: #008080;">var_dump</span>(<span style="color: #008080;">get_class</span>(<span style="color: #800080;">$wangBaoQiang</span>->create1()), <span style="color: #008080;">get_class</span>(<span style="color: #800080;">$wangBaoQiang</span>-><span style="color: #000000;">create2()));
 
</span><span style="color: #008000;">/*</span><span style="color: #008000;">
The result 
string(1) "WangBaoqiang"
string(1) "WangBaoqiang"
</span><span style="color: #008000;">*/</span>
登入後複製

 

 

歡迎大家,批評指正,謝謝

 

來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門推薦
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板