Heute habe ich beim Codieren new static() entdeckt. Ich denke, der Ort der Instanziierung sollte new self() sein, oder? Nach einiger Recherche habe ich den Unterschied zwischen den beiden herausgefunden:
1) Bei Unterklassenintegration verhalten sich die beiden unterschiedlich
2) PHP 5.2 und niedrigere Versionen unterstützen nicht die neue static()-Syntax
具体解释如下: 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() ).
Code hochladen:
<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>
Aber wenn Sie möchten, dass die Unterklasse get_class verwendet, wird auch der Name der aktuellen Unterklasse ('wangbaoqiang') zurückgegeben. Was sollten Sie tun?
<?<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>
Willkommen bei allen, die uns kritisieren und korrigieren, vielen Dank