The difference between new static() and new self() in PHP
self: always points to the class itself where the self code is located. No matter how many times this class is inherited, self points to the class that originally used self;
static: Points to a class that uses static. Only through inheritance can the meaning of static exist. Otherwise, static is the same as 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>
Question:
1. I don’t know what the use of new static() and new self() are;
2. If you want to use new self(), why not use new A();
The above has introduced the difference between new static and new self in PHP, including the relevant aspects. I hope it will be helpful to friends who are interested in PHP tutorials.