PHP static delay statically binding usage analysis _php tips

Source: Internet
Author: User

The example in this article describes the PHP static lazy binding usage. Share to everyone for your reference, specific as follows:

PHP5.3 later introduced a deferred static binding static, what is it to solve the problem? One long-standing problem with PHP's inheritance model is that it is difficult to refer to the final state of an extended class in a parent class. Take a look at an example.

Class A 
{public 
  static function Echoclass () { 
    echo __class__; 
  }
  public static function test () { 
    self::echoclass ();    
  }
}
Class B extends A 
{public    
  static function Echoclass () 
  { 
     echo __class__; 
  } 
} 
B::test (); Output A

A new feature has been added to the PHP5.3: deferred static binding, which is the expression or variable that is fixed at the defining stage, is decided at the execution stage, such as when a subclass inherits the static expression of the parent class, its value cannot be changed, and sometimes it is not desirable to see it.

The following example solves the problem raised above:

Class A 
{public 
  static function Echoclass () { 
    echo __class__; 
  } 
  public static function test () 
  { 
    static::echoclass ();    
  } 
} 
Class B extends A 
{public    
  static function Echoclass () { 
     echo __class__; 
  } 
} 
B::test (); Output b

The Static::echoclass () in line 8th defines a static deferred binding method until B invokes test to execute the method that was originally defined.

For more information on PHP-related content readers can view the site topics: "PHP operation Office Document tips summary (including word,excel,access,ppt)", "PHP date and Time usage summary", "PHP object-oriented Program Design Introductory Course", "PHP string (string) Summary of usage, Getting Started tutorial on Php+mysql database operations, and summary of common PHP database operations techniques

I hope this article will help you with the PHP program design.

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.