"PHP Classes and objects" late static binding

Source: Internet
Author: User

This article introduces the content of "PHP class and object" Late static binding, has a certain reference value, now share to everyone, the need for friends can refer to

Late static binding

PHP 5.3.0

For: Classes in the inheritance scope that refer to static calls

Working principle:

    • The class name stored in the previous "non-forwarding call" (Non-forwarding calls).

    • When a static method call is made, the class name is the one explicitly specified (usually in the left part of the: operator);

    • When a non-static method call is made, the class to which the object belongs.

Forwarding calls (forwarding call): Static calls through Self::,parent::,static::, Forward_static_call () (Calling a static method).
You can use the Get_called_class () function to get the class name of the method being called, Static:: It indicates its scope.

This feature is named "Late static binding" from the perspective of the language itself.
Late binding means that static:: It is no longer parsed to define the class in which the current method resides, but is calculated at the actual run time.
It can also be called a "static binding" because it can be used (but not limited to) the invocation of a static method.

<?phpclass A {public    static function who () {        echo __class__;//Returns the name of the class when it is defined    } public    static function t EST () {        self::who ();    }} Class B extends A {    //public static function who () {    //     echo __class__;    }}b::test ();  A?>

Use of late static bindings

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.

Example #2 static:: Simple usage <?phpclass A {public    static function who () {        echo __class__;    }    public static function test () {        static::who ();//note using static, late static binding starts here    }}class B extends A {public    stat IC function who () {        echo __class__;    }} B::test ();  B?>

In a non-static environment, the class that is called is the class to which the object instance belongs. Because $this, which attempts to invoke private methods within the same scope, static:: You might give different results.
Another difference is static:: Only for static properties.

Late static binding

PHP 5.3.0

For: Classes in the inheritance scope that refer to static calls

Working principle:

    • The class name stored in the previous "non-forwarding call" (Non-forwarding calls).

    • When a static method call is made, the class name is the one explicitly specified (usually in the left part of the: operator);

    • When a non-static method call is made, the class to which the object belongs.

Forwarding calls (forwarding call): Static calls through Self::,parent::,static::, Forward_static_call () (Calling a static method).
You can use the Get_called_class () function to get the class name of the method being called, Static:: It indicates its scope.

This feature is named "Late static binding" from the perspective of the language itself.
Late binding means that static:: It is no longer parsed to define the class in which the current method resides, but is calculated at the actual run time.
It can also be called a "static binding" because it can be used (but not limited to) the invocation of a static method.

<?phpclass A {public    static function who () {        echo __class__;//Returns the name of the class when it is defined    } public    static function t EST () {        self::who ();    }} Class B extends A {    //public static function who () {    //     echo __class__;    }}b::test ();  A?>

Use of late static bindings

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.

Example #2 static:: Simple usage <?phpclass A {public    static function who () {        echo __class__;    }    public static function test () {        static::who ();//note using static, late static binding starts here    }}class B extends A {public    stat IC function who () {        echo __class__;    }} B::test ();  B?>

In a non-static environment, the class that is called is the class to which the object instance belongs. Because $this, which attempts to invoke private methods within the same scope, static:: You might give different results. The
other difference is static:: Only for static properties.

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.