cvs: phpdoc /en/language oop5.xml /en/language/oop5 late-static-bindings.xml

From: Date: Sun, 21 Oct 2007 00:48:36 +0000
Subject: cvs: phpdoc /en/language oop5.xml /en/language/oop5 late-static-bindings.xml
Groups: php.doc.cvs 
Request: Send a blank email to doc-cvs+get-247@lists.php.net to get a copy of this message
colder Sun Oct 21 00:48:36 2007 UTC Added files: /phpdoc/en/language/oop5 late-static-bindings.xml Modified files: /phpdoc/en/language oop5.xml Log: First attempt at documenting late static bindings http://cvs.php.net/viewvc.cgi/phpdoc/en/language/oop5.xml?r1=1.13&r2=1.14&diff_format=u Index: phpdoc/en/language/oop5.xml diff -u phpdoc/en/language/oop5.xml:1.13 phpdoc/en/language/oop5.xml:1.14 --- phpdoc/en/language/oop5.xml:1.13 Wed Jun 20 22:24:12 2007 +++ phpdoc/en/language/oop5.xml Sun Oct 21 00:48:36 2007 @@ -1,5 +1,5 @@ <?xml version="1.0" encoding="iso-8859-1"?> -<!-- $Revision: 1.13 $ --> +<!-- $Revision: 1.14 $ --> <chapter xml:id="language.oop5" xmlns="http://docbook.org/ns/docbook"> <title>Classes and Objects (PHP 5)</title> @@ -30,6 +30,7 @@ &language.oop5.object-comparison; &language.oop5.reflection; &language.oop5.typehinting; + &language.oop5.late-static-bindings; </chapter> <!-- Keep this comment at the end of the file http://cvs.php.net/viewvc.cgi/phpdoc/en/language/oop5/late-static-bindings.xml?view=markup&rev=1.1 Index: phpdoc/en/language/oop5/late-static-bindings.xml +++ phpdoc/en/language/oop5/late-static-bindings.xml <?xml version="1.0" encoding="iso-8859-1"?> <!-- $Revision: 1.1 $ --> <sect1 xml:id="language.oop5.late-static-bindings" xmlns="http://docbook.org/ns/docbook"> <title>Late Static Bindings</title> <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. </para> <para> This feature was named "late static bindings" with an internal perspective in mind. "Late binding" comes from the fact that <literal>static::</literal> will no longer 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. </para> <sect2 xml:id="language.oop5.late-static-bindings.self"> <title>Limitations of <literal>self::</literal></title> <para> Static references to the current class like <literal>self::</literal> or <literal>__CLASS__</literal> are resolved using the class in which the function belongs, as in where it was defined: </para> <example> <title><literal>self::</literal> usage</title> <programlisting role="php"> <![CDATA[ <?php class 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(); ?> ]]> </programlisting> &example.outputs; <screen> <![CDATA[ A ]]> </screen> </example> </sect2> <sect2 xml:id="language.oop5.late-static-bindings.usage"> <title>Late Static Bindings' usage</title> <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 <literal>B</literal> from <literal>test()</literal> in the previous example. It was decided not to introduce a new keyword but rather use <literal>static</literal> that was already reserved. </para> <example> <title><literal>static::</literal> simple usage</title> <programlisting role="php"> <![CDATA[ <?php class A { public static function who() { echo __CLASS__; } public static function test() { static::who(); // Here comes Late Static Bindings } } class B extends A { public static function who() { echo __CLASS__; } } B::test(); ?> ]]> </programlisting> &example.outputs; <screen> <![CDATA[ B ]]> </screen> </example> <note> <para> <literal>static::</literal> does not work like <literal>$this</literal> for static methods! <literal>$this-></literal> follows the rules of inheritance while <literal>static::</literal> doesn't. This difference is detailed later on this manual page. </para> </note> <example> <title><literal>static::</literal> usage in a non-static context</title> <programlisting role="php"> <![CDATA[ <?php class TestChild extends TestParent { public function __construct() { static::who(); } public function test() { $o = new TestParent(); } public static function who() { echo __CLASS__."\n"; } } class TestParent { public function __construct() { static::who(); } public static function who() { echo __CLASS__."\n"; } } $o = new TestChild; $o->test(); ?> ]]> </programlisting> &example.outputs; <screen> <![CDATA[ TestChild TestParent ]]> </screen> </example> <note> <para> Late static bindings' reslution will stop at a fully resolved static call with no fallback. </para> <example> <title>Fully resolved static calls</title> <programlisting role="php"> <![CDATA[ <?php class A { public static function foo() { static::who(); } public static function who() { echo __CLASS__."\n"; } } class B extends A { public static function test() { A::foo(); } public static function who() { echo __CLASS__."\n"; } } B::test(); ?> ]]> </programlisting> &example.outputs; <screen> <![CDATA[ A ]]> </screen> </example> </note> </sect2> <sect2 xml:id="language.oop5.late-static-bindings.edge-cases"> <title>Edge cases</title> <para> 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. </para> <example> <title>Late static bindings inside magic methods</title> <programlisting role="php"> <![CDATA[ <?php class A { protected static function who() { echo __CLASS__."\n"; } public function __get($var) { return static::who(); } } class B extends A { protected static function who() { echo __CLASS__."\n"; } } $b = new B; $b->foo; ?> ]]> </programlisting> &example.outputs; <screen> <![CDATA[ B ]]> </screen> </example> </sect2> </sect1> <!-- Keep this comment at the end of the file Local variables: mode: sgml sgml-omittag:t sgml-shorttag:t sgml-minimize-attributes:nil sgml-always-quote-attributes:t sgml-indent-step:1 sgml-indent-data:t indent-tabs-mode:nil sgml-parent-document:nil sgml-default-dtd-file:"../../manual.ced" sgml-exposed-tags:nil sgml-local-catalogs:nil sgml-local-ecat-files:nil End: vim600: syn=xml fen fdm=syntax fdl=2 si vim: et tw=78 syn=sgml vi: ts=1 sw=1 -->

« previous php.doc.cvs (#247) next »