An example of how to define and use static bindings after PHP

Source: Internet
Author: User
Original document: since PHP 5.3.0, PHP has added a feature called post-static binding, the class used to reference static calls within the inheritance scope.

Late static binding works by storing the class name on the previous "non-forwarding call" (Non-forwarding calls). When a static method call is made, the class name is the one that is explicitly specified (usually in the left part of the: operator), and the class to which the object belongs when a non-static method call is made. The so-called "forwarding calls" (forwarding call) refer to static calls made in the following ways: self::,parent::,static:: and Forward_static_call (). 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.

Self:: the limit

Use self:: or __class__ a static reference to the current class, depending on the class in which the current method is defined:

Example #1 Self:: Usage


<?phpclass A {public static function who () {echo __class__;} public static function test () {self::who ();}} Class B extends A {public static function who () {echo __class__;}} B::test ();? >

The above routines will output:

A

Late use of static bindings later static binding this is to bypass the restriction by introducing a new keyword that represents the class that the runtime originally called. 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 ();//Late static binding starts here}}class B extends A {public static function who () {echo __clas s__;}} B::test ();? >

The above routines will output:

B

Note: 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.

Example #3 use static in a non-static environment:


<?phpclass A {Private Function foo () {echo "success!\n";} Public Function test () {$this->foo (); Static::foo ();}} Class B extends A {/* foo () is copied to B, hence its scope would still be A and* the call being successful */}class C ex Tends A {private Function foo () {/* original method is replaced; the scope of the new one is C */}} $b = new B (); $b->tes T (); $c = new C (); $c->test (); Fails?>

The above routines will output:

success!
success!
success!
Fatal Error:call to Private Method C::foo () from the context ' A ' in/tmp/test.php on line 9

Note: Parsing of a late static binding continues until a fully resolved static call is made. On the other hand, if a static call uses parent:: or self:: The call message is forwarded.

Example #4 forwarding and non-forwarding calls


<?phpclass A {public static function foo () {static::who ();} public static function who () {echo __class__. \ n ";}} Class B extends A {public static function test () {A::foo ();p arent::foo (); Self::foo ();} public static function who () {echo __class__. \ n ";}} Class C extends B {public static function who () {echo __class__. \ n ";}} C::test ();? >

The above routines will output:

A
C
C

The following example parses a class that references static calls in an inherited scope based on the late PHP static binding feature.

Let's look at the following code:


Class Person{public static function status () {Self::getstatus ();} protected static function GetStatus () {echo "person is Alive";}} Class deceased extends person{protected static function GetStatus () {echo "person is deceased";}} Deceased::status (); Person is alive

Obviously, the result is not what we expected, because self: depends on the class in which it was defined, not the class in which it was run. To solve this problem, you might override the status () method in the inheriting class, and a better solution would be to add post-static binding functionality after PHP 5.3.

The code is as follows:


Class Person{public static function status () {Static::getstatus ();} protected static function GetStatus () {echo "person is Alive";}} Class deceased extends person{protected static function GetStatus () {echo "person is deceased";}} Deceased::status (); Person is deceased

Visible, Static:: Does not point to the class that is currently in, in fact, it is evaluated in the run, forcing all properties of the final class to be obtained.

Therefore, it is recommended that you do not use self again::, use static::

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.