PHP static delay statically binding _php tutorial

Source: Internet
Author: User
If you are a lazy programmer, you see the following code may be annoyed

Abstract class u{

}

Class U1 extends u{

public static function Create () {

return new U1 ();

}

}

Class U2 extends u{

public static function Create () {

return new U2 ();

}

}

This piece of code works fine, but a lot of repetitive code can be annoying.

I don't want to add the Create method to each subclass, and if you put the Create method in the superclass U, the code might be

Abstract class u{

public static function Create () {

return new self ();

}

}

Class U1 extends u{

function A () {}

}

Class U2 extends u{

}

U1::create ();

It looks neat and tidy, now we put common code in one place and self as a reference to that class. But here we have a hypothesis about self.

In fact, Self's role in the class is not exactly the same as $this's effect on the object. Self is not referring to the invocation context, he refers to the parsing context, so if you run the above case, you will get

Fatal Error:cannot Instantiate abstract class U in D:\wamp\www\test\oop\static.php on line 21

Self is therefore parsed to define the U of Create, rather than the U1 class that resolves to call self.

Prior to php5.3, there were strict restrictions in this area, resulting in a lot of awkward solutions, php5.3 introduced lazy static binding and the use of the keyword static

Static is similar to self, but it refers to the class being called instead of the containing class.

In the following example U1::create will generate the U1 object instead of instantiating the U object

Abstract class u{

public static function Create () {

return new static ();

}

}

Class U1 extends u{

}

Class U2 extends u{

}

U1::create ();

Static can be used not only for instantiation, but also as a call identifier for a static method, even from a non-static context, as self,parent.

Abstract class u{

Private $group;

Public Function __construct () {

$this->group=static::getgroup ();

}

public static function Create () {

return new static ();

}

static function Getgroup () {

return ' default ';

}

}

Class U1 extends u{

}

Class U2 extends u{

static function Getgroup () {

Return ' U2 ';

}

}

Class U3 extends u2{

}

Print_r (U1::create ());

Echo '
';

Print_r (U3::create ());

U1 Object ([group:u:private] = default)

U3 Object ([group:u:private] = U2)

http://www.bkjia.com/PHPjc/742266.html www.bkjia.com true http://www.bkjia.com/PHPjc/742266.html techarticle If you are a lazy programmer, you see the following code may annoy abstract class u{} class U1 extends u{public static function create () {return new U1 ();} } class U2 E ...

  • 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.