From b579b724d18c9e15d81cf5604be811f0c687bce9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustavo=20Andr=C3=A9=20dos=20Santos=20Lopes?= Date: Sun, 19 Sep 2010 06:58:29 +0000 Subject: [PATCH] - More precise description on how LSB works. - Differences between $this-> and static:: in non-static contexts. - Removed vague "edge cases" section, with an example that was not really surprising. git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@303545 c90b9560-bf6c-de11-be94-00142212c4b1 --- language/oop5/late-static-bindings.xml | 115 ++++++++++--------------- 1 file changed, 46 insertions(+), 69 deletions(-) diff --git a/language/oop5/late-static-bindings.xml b/language/oop5/late-static-bindings.xml index fdfd5d93b8..f99c656f6c 100644 --- a/language/oop5/late-static-bindings.xml +++ b/language/oop5/late-static-bindings.xml @@ -7,11 +7,27 @@ can be used to reference the called class in a context of static inheritance. + + More precisely, late static bindings work by storing the class named in the + last "non-forwarding call". In case of static method calls, this is the + class explicitly named (usually the one on the left of the + :: + operator); in case of non static method calls, it is the class of the object. A + "forwarding call" is static one that is introduced by self::, + parent::, static::, or, if going + up in the class hierarchy, forward_static_call. + + + The function get_called_class can be used to retrieve + a string with the name of the called class and static:: + introduces its scope. + + This feature was named "late static bindings" with an internal perspective in mind. "Late binding" comes from the fact that static:: - will no longer be resolved using the class where the method is defined but - it will rather be computed using runtime information. + will not be resolved using the class where the method is defined but it will + rather be computed using runtime information. It was also called a "static binding" as it can be used for (but is not limited to) static method calls. @@ -103,10 +119,11 @@ B - static:: does not work like $this for - static methods! $this-> follows the rules of - inheritance while static:: doesn't. This difference is - detailed later on this manual page. + Note that, in non-static contexts, the called class will be the same as + the class the object. Since $this-> will try to call + private methods from the same scope, using static:: may + give different results. Another difference is that + static:: can only refer to static properties. @@ -114,40 +131,43 @@ B foo(); + static::foo(); } } -class TestParent { - public function __construct() { - static::who(); - } +class B extends A { + /* foo() will be copied to B, hence its scope will still be A and + * the call be successful */ +} - public static function who() { - echo __CLASS__."\n"; +class C extends A { + private function foo() { + /* original method is replaced; the scope of the new one is C */ } } -$o = new TestChild; -$o->test(); +$b = new B(); +$b->test(); +$c = new C(); +$c->test(); //fails ?> ]]> &example.outputs; @@ -205,50 +225,6 @@ C - - Edge cases - - There are lots of different ways to trigger a method call in PHP, like - callbacks or magic methods. As late static bindings base their resolution - on runtime information, it might give unexpected results in so-called edge - cases. - - - Late static bindings inside magic methods - -foo; -?> -]]> - - &example.outputs; - - - - - +