PHP Static delay statically binding

Source: Internet
Author: User
Tags abstract object return
If you are a lazy programmer, you see the following code may be annoyed     abstract  class u{    } class U1 extends u{  &NBSP;PU Blic static function Create () {     return new U1 ();  }      }   class U2 E Xtends u{   public static function Create () {     return new U2 ();    }}   This code is Regular work is no problem, but a lot of repetitive code can be annoying   I don't want to add a create method to each subclass, and if you put the Create method in superclass U, the code might be     abstract class u{  public The static function create () {   return new self ()  }} class U1 extends u{  function A () {}}   CL Ass U2 extends u{ } u1::create ();   looks neat and tidy, now we put the common code in one place and use self as a reference to that class. But here we make a hypothesis about self.   In effect, self does not have exactly the same effect on the class as $this does on the object. Self refers not to the calling context, he refers to the parsing context, so if you run the above, you will get the   Fatal Error:cannot instantiate abstract class U in D:\wamp\www\test\oop \static.php on line   So self is resolved to define U for create instead of parsing to the U1 class that invokes self.   php5.3 before, there are strict restrictions on this, resulting in a number of clumsy solutions, php5.3 introduced a delayed static bindingSet   and use the keyword  static   static like self, but it refers to the called class instead of the containing class.   In the following example U1::create will generate U1 objects instead of instantiating the U object     Abstract class u{  public static function create () {  &N Bsp;return new Static ();  } class U1 extends u{}   class U2 extends u{ } u1::create ();   Static can be used not only as an instantiation, but also as a call identifier for a static method, or even from a non-static context self,parent         abstract class u{  Private $group;   Public Function __construct () {      $this->group=static::getgroup (),  }   public stat IC function Create () {   return new static ()  }   static function Getgroup () {     re Turn ' default ';  } class U1 extends u{}   class U2 extends u{ static function Getgroup () {   return ' U2 '; &n BSP;} Class U3 extends u2{ } print_r (U1::create ()); Echo ' <br/> '; Print_r (U3::create ());   U1 object ([group:u:private] => default)   U3 object ([GROUP:U:PRIvate] => U2)

Related Article

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.