PHP lazy Static binding use detailed

Source: Internet
Author: User
Tags php class
This time for everyone to bring PHP delay static binding use of the details, PHP delay static binding use of considerations, the following is the actual case, take a look.

The so-called lazy static binding, as the name implies, is called statically: the binding of the part to the left of the symbol is a delay, that is, it is no longer resolved to define the class in which the current method resides, but is computed at the actual run time. This article mainly introduces about the delay static binding of PHP related content, the following words do not say, come together to see the detailed introduction it.

Smell the bad smell.

This period of time to see the project behind the PHP code, see similar to the following section of code, I drew it out:

<?php class Dbhandler {  function get () {}} class Mysqlhandler extends Dbhandler {  //Here a Create public  STA Tic function Create () {   echo "MySQL";   return new self ();  Public function Get () {   echo "MySQL get ()"  }} class Memcachedhandler extends Dbhandler {  //here is another CREATE
  public static function Create () {   echo "Memcached";   return new self ();  Public function Get () {   echo ' Memcached get ',  }} function get (Dbhandler $handler) {  $handler->get ();} $dbHandler = Mysqlhandler::create (); Get ($dbHandler);? >

Did you smell the bad code? As you can see, in the Mysqlhandler and Memcachedhandler classes, there is a CREATE function that removes my output statements and finds them identical, which is code redundancy. Yes, code refactoring is required.

For a simple refactoring

Code refactoring is everywhere, as long as you think that you have improved, you need to start working on the keyboard. Come on, refactor the code above, as follows:

<?php class Dbhandler {public  static function create () {   echo "create";   return new self ();  function get () {}} class Mysqlhandler extends Dbhandler {public  function get () {   echo "MySQL get ()"  }} CLA SS Memcachedhandler extends Dbhandler {public  function get () {   echo "Memcached get";  }} function Get (Dbhand Ler $handler) {  $handler->get ();} $dbHandler = Mysqlhandler::create (); get ($dbHandler); >

Moving the CREATE function to the Dbhandler class looks pretty good, at least a lump of that bad code.

It seems wrong.

Run, but found, and did not print out what we expected MySQL get() . What's the situation? This means that there is no call to Mysqlhandler's get function, but the code is clearly called, which indicates that new self() there is a problem with this code. What's wrong with that? This needs to be the focus of today's summary ———— delay static binding.

Lazy static binding

Delayed static bindings were introduced after PHP5.3. Let's look at the following code:

<?php class A {public  static function who () {   echo class;  }  public static function test () {   self::who (),  }} class B extends A {public  static function who () {   echo C LASS;  } } b::test ();? >

The above code outputs a, but I want it to output B, which is where the problem lies. This is also the self and CLASS restrictions. Use the self:: or class for a static reference to the current class, depending on where the current method is defined. So, this is a good explanation of why the above code outputs a. But what if we need to output B? Can do this:

<?php class A {public  static function who () {   echo class;  }  public static function test () {   static::who ();//change here, late static binding starts here  }}} class B extends A {public  static F Unction who () {   echo CLASS;  }} B::test ();? >

Late static binding This is to bypass the restriction by introducing a new keyword that represents the class that was originally called by the runtime. Simply put, this keyword allows you to invoke test () in the above example to refer to the class B instead of a. The final decision is not to introduce a new keyword, but instead to use the static keyword that has been reserved.

This is the alternative use of the static keyword at the very end of a late-statically binding ————. For the first example of this article, you can change this:

return new static (); Change here, late static binding

This use of late static binding, when using PHP implementation of 23 design mode, you will feel very easy.

Believe that you have read the case of this article you have mastered the method, more exciting please pay attention to the PHP Chinese network other related articles!

Recommended reading:

PHP based on object-oriented implementation of message This step is detailed

PHP Single Responsibility principle (SRP) use case resolution

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.