note 26720 deleted from function.include by sniper
| From: | sniper@php.net | Date: | Wed, 29 Jan 2003 04:17:11 +0000 |
| Subject: | note 26720 deleted from function.include by sniper | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-42998@lists.php.net to get a copy of this message | ||
I'm trying to design an elegant include dependency structure for a large project. My idea was
using return() in libraries if some dependency wasn't loaded, and have an include manager parse
the result and first include the dependency and then *retry* loading the original file after
including the dependencies. To my unpleasant surprise, class definitions below the return()
statement were loaded by PHP even if the return() did execute above it! The code below the return
statement didn't run though, but since the class definition was loaded, retrying to load the
same file resulted in a PHP error saying that I'm trying to duplicate a class!
For instance:
--- myFile.php ---
<?
include("myLib.php");
if (class_exists("myclass")) {
echo("It's defined!");
}
?>
--- myLib.php ---
<?
return(1);
echo("Whoaa! return() didn't do a thing!");
class myClass
{
function myClass() {}
}
?>
Executing myFile.php *will* echo "It's defined!" but won't echo "Whoaa!
return() didn't do a thing!"
To further complicate things, functions will exhibit the same behaviour, and extending classes of
previously defined classes will also be defined. Extending classes which haven't been
previously defined however will not be defined.
The explanation is simple: PHP 4 allows defining functions and classes *after* the code which uses
them, so PHP must first read class and function definition in every script, and only then will it
execute the code.
Here's a page demonstrating this behaviour with a more extended version of the files listed
above (actual sources also listed): http://bogdan.lanifex.com/PHP_include_test/myFile.php