Home >Backend Development >PHP Tutorial >The difference between new static and new self in PHP, staticself_PHP tutorial
Today the boss asked about the difference between new static and new self in the company, and there was not even one of the ten programs in the company The answer will be added later in the screen. . .
After I got home, I went to Baidu to learn about the difference between the two:
Use self:: or __CLASS__ to statically reference the current class, depending on the class in which the current method is defined:
Using static:: is no longer parsed into the class in which the current method is defined, but is calculated at 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 the class in which it is written, 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 this static is used 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 Liezi
<?<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 study