The official website says:
As of PHP 5.3.0, PHP implements a feature called late static bindings which can be used To reference the called Class In a context of the static inheritance.
more precisely, late static bindings work by storing the class named in the last "Non-forwarding call". In case of a static method calls, this is the class explicitly named (usually the one on the left of the :: &nb Sp;operator); In case of non static method calls, it is the class of the object. A "forwarding call" was a static one that was introduced by self:: , Parent:: , static:: , or, if going up in the class Hierarchy, forward_static_call (). The Function get_called_class () can be used-retrieve a string with the name of the CAL Led class and static:: introduces its scope.
This feature is named "Late static bindings" with a internal perspective in mind. "Late binding" comes from the fact that the static:: would not be resolved using the class where the method is defined But it'll rather be computed using runtime information. It was also called a "static binding" as it can being used for (and but isn't limited to) static method calls.
Limitations of
Self ::¶
Static references to the current class as self :: or __class__ is resolved using the class in which th e function belongs, as in where it is defined:
Example #1 self :: usage,
classA { Public Static functionWho () {Echo __class__; } Public Static functionTest () { self::Who (); }}classBextendsA { Public Static functionWho () {Echo __class__; }}b:: Test ();
Output A.
Late Static Bindings ' usage¶
Late static bindings tries to solve, Limitation by introducing a keyword that references the class it was Initia Lly called at runtime. Basically, a keyword that would allow you to reference B from test () in the previous example. It was decided the not- introduce a new keyword but rather use static the was already reserved.
Example #2 Static:: Simple usage
<?PHPclassA { Public Static functionWho () {Echo __class__; } Public Static functionTest () {Static:: Who ();//Here comes late Static Bindings }}classBextendsA { Public Static functionWho () {Echo __class__; }}b::test ();?>
Output B;
Note:
In non-static contexts, the called class would be the class of the object instance. Since $this , would try to call private methods from the same scope, using static:: may give different Results. Another difference is thisstatic:: can only refer to static properties.
Example #3 static:: usage in a non-static context
classA {Private functionfoo () {Echo"Success!\n"; } Public functionTest () {$this-foo (); Static::foo (); }}classBextendsA {/*foo () would be copied to B, hence its scope would still be A and * the call is successful*/}classCextendsA {Private functionfoo () {/*original method is replaced; the scope of the new one is C*/ }}$b=NewB ();$b-test ();$c=NewC ();$c->test ();//fails
success! success! success!
( ! ) Fatal Error: Call to Private Method C::foo () from context ' A ' In F:\xampp\htdocs\php\phpSyntax\lateStaticBinding . PHP on line 26
More:
Http://cn2.php.net/lsb