Today, the boss asked about the difference between new static and new self at the company. Among the ten programs in the company, not one of them had an answer. I will make up for it later. . .
After I got home, I went to Baidu to learn about the difference between the two:
Use self:: or __CLASS__ A static reference to the current class, depending on the class in which the current method is defined:
Use static:: is no longer parsed into the class in which the current method is defined, but is calculated during actual runtime. It can also be called "static binding" because it can be used for (but is not limited to) calls to static methods.
To put it simply and popularly, self refers to which class it is written in, and it is this class that is actually called. The so-called late static binding, static represents the class used, which is the static you wrote in the parent class,
Then we use this static directly/indirectly through the subclass. This static refers to this subclass, so static is very similar to $this, but static can be used for static methods and properties.
Please see the examples
<?<span>php </span><span>class</span><span> Person { </span><span>public</span><span>static</span><span>function</span><span> name() { </span><span>echo</span> "xiaosan"<span>; } </span><span>public</span><span>static</span><span>function</span><span> callself() { self</span>::<span>name(); } </span><span>public</span><span>static</span><span>function</span><span> callstatic() { </span><span>static</span>::<span>name(); } } </span><span>class</span> Man <span>extends</span><span> Person { </span><span>public</span><span>static</span><span>function</span><span> name() { </span><span>echo</span> "gaojin"<span>; } } Man</span>::name(); <span>//</span><span> output: gaojin</span>Person::callself(); <span>//</span><span> output: xiaosan</span>Person::callstatic(); <span>//</span><span> output:gaojin</span>?>
The editor continues to learn
The above introduces the difference between new static and new self in PHP, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.