<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Late Static Bindings</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="language.oop5.typehinting.html">Type Hinting</a></div> <div class="next" style="text-align: right; float: right;"><a href="language.oop5.references.html">Objects and references</a></div> <div class="up"><a href="language.oop5.html">Classes and Objects</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="language.oop5.late-static-bindings" class="sect1"> <h2 class="title">Late Static Bindings</h2> <p class="para"> 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 static inheritance. </p> <p class="para"> 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 <a href="language.oop5.paamayim-nekudotayim.html" class="link"><em>::</em></a> operator); in case of non static method calls, it is the class of the object. A "forwarding call" is a static one that is introduced by <em>self::</em>, <em>parent::</em>, <em>static::</em>, or, if going up in the class hierarchy, <span class="function"><a href="function.forward-static-call.html" class="function">forward_static_call()</a></span>. The function <span class="function"><a href="function.get-called-class.html" class="function">get_called_class()</a></span> can be used to retrieve a string with the name of the called class and <em>static::</em> introduces its scope. </p> <p class="para"> This feature was named "late static bindings" with an internal perspective in mind. "Late binding" comes from the fact that <em>static::</em> 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. </p> <div class="sect2" id="language.oop5.late-static-bindings.self"> <h3 class="title">Limitations of <em>self::</em></h3> <p class="para"> Static references to the current class like <em>self::</em> or <em>__CLASS__</em> are resolved using the class in which the function belongs, as in where it was defined: </p> <div class="example" id="example-237"> <p><strong>Example #1 <em>self::</em> usage</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #007700">class </span><span style="color: #0000BB">A </span><span style="color: #007700">{<br /> public static function </span><span style="color: #0000BB">who</span><span style="color: #007700">() {<br /> echo </span><span style="color: #0000BB">__CLASS__</span><span style="color: #007700">;<br /> }<br /> public static function </span><span style="color: #0000BB">test</span><span style="color: #007700">() {<br /> </span><span style="color: #0000BB">self</span><span style="color: #007700">::</span><span style="color: #0000BB">who</span><span style="color: #007700">();<br /> }<br />}<br /><br />class </span><span style="color: #0000BB">B </span><span style="color: #007700">extends </span><span style="color: #0000BB">A </span><span style="color: #007700">{<br /> public static function </span><span style="color: #0000BB">who</span><span style="color: #007700">() {<br /> echo </span><span style="color: #0000BB">__CLASS__</span><span style="color: #007700">;<br /> }<br />}<br /><br /></span><span style="color: #0000BB">B</span><span style="color: #007700">::</span><span style="color: #0000BB">test</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p>The above example will output:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> A </pre></div> </div> </div> </div> <div class="sect2" id="language.oop5.late-static-bindings.usage"> <h3 class="title">Late Static Bindings' usage</h3> <p class="para"> Late static bindings tries to solve that limitation by introducing a keyword that references the class that was initially called at runtime. Basically, a keyword that would allow you to reference <em>B</em> from <em>test()</em> in the previous example. It was decided not to introduce a new keyword but rather use <em>static</em> that was already reserved. </p> <div class="example" id="example-238"> <p><strong>Example #2 <em>static::</em> simple usage</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #007700">class </span><span style="color: #0000BB">A </span><span style="color: #007700">{<br /> public static function </span><span style="color: #0000BB">who</span><span style="color: #007700">() {<br /> echo </span><span style="color: #0000BB">__CLASS__</span><span style="color: #007700">;<br /> }<br /> public static function </span><span style="color: #0000BB">test</span><span style="color: #007700">() {<br /> static::</span><span style="color: #0000BB">who</span><span style="color: #007700">(); </span><span style="color: #FF8000">// Here comes Late Static Bindings<br /> </span><span style="color: #007700">}<br />}<br /><br />class </span><span style="color: #0000BB">B </span><span style="color: #007700">extends </span><span style="color: #0000BB">A </span><span style="color: #007700">{<br /> public static function </span><span style="color: #0000BB">who</span><span style="color: #007700">() {<br /> echo </span><span style="color: #0000BB">__CLASS__</span><span style="color: #007700">;<br /> }<br />}<br /><br /></span><span style="color: #0000BB">B</span><span style="color: #007700">::</span><span style="color: #0000BB">test</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p>The above example will output:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> B </pre></div> </div> </div> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> In non-static contexts, the called class will be the class of the object instance. Since <em>$this-></em> will try to call private methods from the same scope, using <em>static::</em> may give different results. Another difference is that <em>static::</em> can only refer to static properties. </p> </p></blockquote> <div class="example" id="example-239"> <p><strong>Example #3 <em>static::</em> usage in a non-static context</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #007700">class </span><span style="color: #0000BB">A </span><span style="color: #007700">{<br /> private function </span><span style="color: #0000BB">foo</span><span style="color: #007700">() {<br /> echo </span><span style="color: #DD0000">"success!\n"</span><span style="color: #007700">;<br /> }<br /> public function </span><span style="color: #0000BB">test</span><span style="color: #007700">() {<br /> </span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">foo</span><span style="color: #007700">();<br /> static::</span><span style="color: #0000BB">foo</span><span style="color: #007700">();<br /> }<br />}<br /><br />class </span><span style="color: #0000BB">B </span><span style="color: #007700">extends </span><span style="color: #0000BB">A </span><span style="color: #007700">{<br /> </span><span style="color: #FF8000">/* foo() will be copied to B, hence its scope will still be A and<br /> * the call be successful */<br /></span><span style="color: #007700">}<br /><br />class </span><span style="color: #0000BB">C </span><span style="color: #007700">extends </span><span style="color: #0000BB">A </span><span style="color: #007700">{<br /> private function </span><span style="color: #0000BB">foo</span><span style="color: #007700">() {<br /> </span><span style="color: #FF8000">/* original method is replaced; the scope of the new one is C */<br /> </span><span style="color: #007700">}<br />}<br /><br /></span><span style="color: #0000BB">$b </span><span style="color: #007700">= new </span><span style="color: #0000BB">B</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$b</span><span style="color: #007700">-></span><span style="color: #0000BB">test</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$c </span><span style="color: #007700">= new </span><span style="color: #0000BB">C</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$c</span><span style="color: #007700">-></span><span style="color: #0000BB">test</span><span style="color: #007700">(); </span><span style="color: #FF8000">//fails<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p>The above example will output:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> success! success! success! Fatal error: Call to private method C::foo() from context 'A' in /tmp/test.php on line 9 </pre></div> </div> </div> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> Late static bindings' resolution will stop at a fully resolved static call with no fallback. On the other hand, static calls using keywords like <em>parent::</em> or <em>self::</em> will forward the calling information. </p> <div class="example" id="example-240"> <p><strong>Example #4 Forwarding and non-forwarding calls</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #007700">class </span><span style="color: #0000BB">A </span><span style="color: #007700">{<br /> public static function </span><span style="color: #0000BB">foo</span><span style="color: #007700">() {<br /> static::</span><span style="color: #0000BB">who</span><span style="color: #007700">();<br /> }<br /><br /> public static function </span><span style="color: #0000BB">who</span><span style="color: #007700">() {<br /> echo </span><span style="color: #0000BB">__CLASS__</span><span style="color: #007700">.</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /> }<br />}<br /><br />class </span><span style="color: #0000BB">B </span><span style="color: #007700">extends </span><span style="color: #0000BB">A </span><span style="color: #007700">{<br /> public static function </span><span style="color: #0000BB">test</span><span style="color: #007700">() {<br /> </span><span style="color: #0000BB">A</span><span style="color: #007700">::</span><span style="color: #0000BB">foo</span><span style="color: #007700">();<br /> </span><span style="color: #0000BB">parent</span><span style="color: #007700">::</span><span style="color: #0000BB">foo</span><span style="color: #007700">();<br /> </span><span style="color: #0000BB">self</span><span style="color: #007700">::</span><span style="color: #0000BB">foo</span><span style="color: #007700">();<br /> }<br /><br /> public static function </span><span style="color: #0000BB">who</span><span style="color: #007700">() {<br /> echo </span><span style="color: #0000BB">__CLASS__</span><span style="color: #007700">.</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /> }<br />}<br />class </span><span style="color: #0000BB">C </span><span style="color: #007700">extends </span><span style="color: #0000BB">B </span><span style="color: #007700">{<br /> public static function </span><span style="color: #0000BB">who</span><span style="color: #007700">() {<br /> echo </span><span style="color: #0000BB">__CLASS__</span><span style="color: #007700">.</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /> }<br />}<br /><br /></span><span style="color: #0000BB">C</span><span style="color: #007700">::</span><span style="color: #0000BB">test</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p>The above example will output:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> A C C </pre></div> </div> </div> </p></blockquote> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="language.oop5.typehinting.html">Type Hinting</a></div> <div class="next" style="text-align: right; float: right;"><a href="language.oop5.references.html">Objects and references</a></div> <div class="up"><a href="language.oop5.html">Classes and Objects</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>