Re: note 26720 added to function.include
| From: | Maxim Maletsky | Date: | Sun, 10 Nov 2002 18:25:04 +0000 |
| Subject: | Re: note 26720 added to function.include | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-39267@lists.php.net to get a copy of this message | ||
Done it. It is kind of long. I wonder whether others will bare with the
note because it is also our policy not to get the articles in but little
*notes*.
Thanks for all, Bogdan
--
Maxim Maletsky
maxim@php.net
On 10 Nov 2002 17:34:41 -0000 mgv@fx.ro wrote:
> 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): https://bogdan.lanifex.com/PHP_include_test/myFile.php
> --
> http://www.php.net/manual/en/function.include.php
> http://master.php.net/manage/user-notes.php?action=edit+26720
> http://master.php.net/manage/user-notes.php?action=delete+26720
> http://master.php.net/manage/user-notes.php?action=reject+26720
>
>
> --
> PHP Notes Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>