Maison > php教程 > php手册 > PHP中new static()与new self()的比较

PHP中new static()与new self()的比较

WBOY
Libérer: 2016-08-20 08:47:38
original
853 Les gens l'ont consulté

  今天在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() ).
Copier après la connexion

上代码:

<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>
Copier après la connexion

但是如果想让 子类使用 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>
Copier après la connexion

 

 

欢迎大家,批评指正,谢谢

 

Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Recommandations populaires
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal