When I was coding today, I discovered new static(). I think the instantiation place should be new self(), right? After some research, I found out the difference between the two:
1) When there is subclass integration, the performance of the two is different
2) PHP 5.2 and below versions do not support the new 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() ).
Upload code:
<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>
But if you want the subclass to use get_class, the name of the current subclass ('wangbaoqiang') will also be returned, what should you do?
ff0b9943638191ca2ce0219679ece523create1()), <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>
Welcome everyone to criticize and correct, thank you