PHP中new static()与new self()的区别
self:始终指向self代码所在类的本身,无论这个类被继承了多少次,self都指向最初使用self的类;
static:指向使用static的类,只有通过继承后,才能体现出static存在的意义,否则static和self一样。
<code><span><span>class</span><span>A</span> {</span><span>public</span><span><span>function</span><span>getStatic</span><span>()</span>{</span><span>return</span><span>new</span><span>static</span>(); } <span>public</span><span><span>function</span><span>getSelf</span><span>()</span>{</span><span>return</span><span>new</span><span>self</span>(); } } <span><span>class</span><span>B</span><span>extends</span><span>A</span>{</span>} var_dump((<span>new</span> B())->getSelf());<span>//A</span> var_dump((<span>new</span> B())->getStatic());<span>//B</span></code>
疑问:
1、不知道new static()和new self()有什么用;
2、如果要使用new self(),为何不使用new A();
以上就介绍了PHP中new static与new self的区别,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。