The difference between new static and new self in PHP

Source: Internet
Author: User
Today, the boss in the company asked a new static and new self of the difference between the company 10 programs did not have an answer up the back of the screen to fill ...

This cock silk home after Baidu understand the following two differences:

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 resolved to define the class in which the current method resides, but is calculated at the actual run time. It can also be called a "static binding" because it can be used (but not limited to) the invocation of a static method.

In simple terms, self is written in which class, which is actually called the class. The so-called late static binding, static represents the class used, is the static you write in the parent class,

This static is then used directly/indirectly through the subclass, which is the subclass, so static and $this are much like, but static can be used for methods and properties.

Look at the list.

 
  PHPclass  person{    publicstaticfunction  name ()    {         echo "Xiaosan";    }      Public Static function callself ()    {        self::name ();    }      Public Static function callstatic ()    {        static::name ();    }} class extends person{    publicstaticfunction  name ()    {         Echo "Gaojin";    }} man:: Name ();  // Output:gaojin Person::callself ();  // Output:xiaosan Person::callstatic ();  // Output:gaojin?>

Keep Learning in small series

The above introduces the difference between new static and new self in PHP, including the contents of the content, I hope that the PHP tutorial interested in a friend helpful.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.