PHP share 22: PHP Object-oriented

Source: Internet
Author: User

1:static Access character

    • Using static in a class has two main uses, defining static members, and defining static methods. Static members retain only one variable value, which is valid for all instances
    • The method of the class is static, and the property that he accesses must also be static

2:static late Static binding

What is static binding?

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 ();  Self will make an error, and self's role in the class is not exactly the same as $this's effect on the object. Self is not referring to the calling context, he refers to the parsing context,
So if you run the above, you'll get
Fatal Error:cannot Instantiate abstract class U in d:wampwwwtestoopstatic.php on line 21
Self is therefore parsed to define the U of Create, rather than the U1 class that resolves to call self.

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.
return new Static ();} } Class U1 extends U{}class U2 extends u{} $res = U1::create (); Var_dump ($res);

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 ());

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

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

Example:

Class A {public    static function foo () {        static::who ();    }     public static function who () {        echo __class__. \ n ";    }} Class B extends A {public    static function test () {        a::foo ();  If you change to static, then bind backwards, and if it is a, the lowest class is a        Parent::foo ();        Self::foo ();    }     public static function who () {        echo __class__. \ n ";    }} Class C extends B {public    static function who () {        echo __class__. \ n ";    }} C::test ();

Output: A c c

PHP share 22: PHP Object-oriented

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.