Home > Backend Development > PHP Tutorial > The difference between new static and new self in PHP, staticself_PHP tutorial

The difference between new static and new self in PHP, staticself_PHP tutorial

WBOY
Release: 2016-07-13 09:45:33
Original
872 people have browsed it

The difference between new static and new self in PHP, staticself

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>
?>
Copy after login

The editor continues to study

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1040164.htmlTechArticleThe difference between new static and new self in PHP, staticself Today, the boss asked about new static and new self at the company Among the ten programs of the Difference Company, none of the answers came up on the back screen...
Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template