[thelist] managing php include dependencies?

Ivo evolt-thelist at sinchi.org
Wed Apr 11 16:42:26 CDT 2007


Thanks for the advice, I was able to solve the problem.

Seems that the order in which files are specified with include_once/require_once does not guarantee parsing order (with the exception that files will not be parsed again if included multiple times). Does this sound correct? I did not find in the documentation anything related to this but perhaps I am overlooking it.

I have a lot of php files that are standalone components and they can be used together to build more complex components. To satisfy dependencies and to prevent double inclusions I was relying on include_once / require_once at the top of each file. This creates huge web of dependencies for files that consist of multiple components which in turn are based on the same components but different arrangements.

I was able to force parsing order by using

if(! defined('configuration.php')) require('configuration');

And this revealed bugs due to circular dependencies.

I guess PHP doesnt parse a file & its includes sequentially but rather builds a tree of all included files in a code path and they looks for the best parsing order to satisfy all dependencies. Since there were two circular dependencies in the code path I was having problems with the parsing path was less than optimal.



----- Original Message ----
From: kasimir-k <kasimir.k.lists at gmail.com>
To: "thelist at lists.evolt.org" <thelist at lists.evolt.org>
Sent: Wednesday, April 11, 2007 12:21:12 PM
Subject: Re: [thelist] managing php include dependencies?

Ivo scribeva in 11/04/2007 16:36:
> In status.html: constant not yet set TEST_CONSTANT is: TEST_CONSTANT
> In configuration.php: set constant TEST_CONSTANT to: "**a test string**"
> In status.html: set constant TEST_CONSTANT is:  "**a test string**"
> In selector.php: constant TEST_CONSTANT is: **a test string**
> In logger.php" constant TEST_CONSTANT is: TEST_CONSTANT
> --
> status.html, selector.php & logger.php use include_once('configuration.php')

Based on this, to me it seems pretty clear that logger.php is in a 
different directory than the other files, and configuration.php can not 
be found from its directory with your include_path.

But why are you using include_once in the first place, if the file 
contains something that you *must* have included? In those cases you 
should use require_once, which will produce a fatal error if the file 
could not be included - then you very much know, where the inclusion 
does not happen. Include will only give you a warning.

Oh, and the page in the fine manual you want to read again for further 
details is <http://php.net/include/> ;-)


* * Please support the community that supports you.  * *

For unsubscribe and other options, including the Tip Harvester 
and archives of thelist go to: http://lists.evolt.org 
Workers of the Web, evolt ! 

More information about the thelist mailing list