note 108678 modified in keyword.class by danbrown

From: Date: Thu, 17 May 2012 14:51:42 +0000
Subject: note 108678 modified in keyword.class by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-188036@lists.php.net to get a copy of this message
You can invoke a class above its definitions in some cases, but there are very important exceptions to this behavior. If your class extends another class, and the interpreter doesn&#39;t see the extended class first, then it won&#39;t be added to the symbol table until the code has stepped through it in runtime. As a result, if you try to invoke it above the definition, you&#39;ll get a &#34;class not found&#34; fatal error.&#13;&#10;&#13;&#10;And those suck.&#13;&#10;&#13;&#10;So, to provide an example, the following will output a fatal error&#13;&#10;&#60;?php&#13;&#10;Bar::bar();&#13;&#10;exit;&#13;&#10;&#13;&#10;class Bar extends Foo { } //will fatal because Foo not seen yet&#13;&#10;&#13;&#10;class Foo {&#13;&#10; static function bar() { echo &#39;yep, this is Foo::bar&#39;; }&#13;&#10;}&#13;&#10;?&#62;&#13;&#10;&#13;&#10;However, THIS code will work just fine:&#13;&#10;&#60;?php&#13;&#10;Bar::bar();&#13;&#10;exit;&#13;&#10;&#13;&#10;class Foo {&#13;&#10; static function bar() { echo &#39;yep, this is Foo::bar&#39;; }&#13;&#10;}&#13;&#10;&#13;&#10;class Bar extends Foo { } //will work because Foo came first&#13;&#10;?&#62;&#13;&#10;&#13;&#10;Notice that if you include a file containing the class you will extend, and then extend the class in the same file as its invocation, you can also get the class not found fatal. This happens even if the &#39;include&#39; call happens before the child class&#39;s definition.&#13;&#10;&#13;&#10;Eg. the following will also output a fatal error&#13;&#10;&#60;?php&#13;&#10;include_once(&#39;file_with_foo.php&#39;);&#13;&#10;&#13;&#10;Bar::bar();&#13;&#10;exit;&#13;&#10;&#13;&#10;class Bar extends Foo { }&#13;&#10;?&#62;&#13;&#10;&#13;&#10;Hope that clarifies things. --was-- I want to add a comment about what pov at fingerprint.fr was saying, because while his examples do exactly what they say, they can also lead to some flawed conclusions about how PHP behaves. You can invoke a class above its definitions in some cases, but there are very important exceptions to this behavior. If your class extends another class, and the interpreter doesn't see the extended class first, then it won't be added to the symbol table until the code has stepped through it in runtime. As a result, if you try to invoke it above the definition, you'll get a "class not found" fatal error. And those suck. So, to provide an example, the following will output a fatal error <?php Bar::bar(); exit; class Bar extends Foo { } //will fatal because Foo not seen yet class Foo { static function bar() { echo 'yep, this is Foo::bar'; } } ?> However, THIS code will work just fine: <?php Bar::bar(); exit; class Foo { static function bar() { echo 'yep, this is Foo::bar'; } } class Bar extends Foo { } //will work because Foo came first ?> Notice that if you include a file containing the class you will extend, and then extend the class in the same file as its invocation, you can also get the class not found fatal. This happens even if the 'include' call happens before the child class's definition. Eg. the following will also output a fatal error <?php include_once('file_with_foo.php'); Bar::bar(); exit; class Bar extends Foo { } ?> Hope that clarifies things. And thanks pov at fingerprint.fr for bringing up this topic in the docs. I agree it's an important point to be aware of. http://php.net/manual/en/keyword.class.php

« previous php.notes (#188036) next »