[clug-progsig] Unique file include problem with PHP?

Shawn sgrover at open2space.com
Mon Aug 22 22:20:37 PDT 2005


I have a situation where I'm looking through an array, and need to process 
each element.  The processing requires calling similar code in some include 
files.  The files to be included are determined at run time.  Each of the 
include files makes use of an array, which MUST have a common name.

So, the code looks something like so:

#this line is simply to show the processing - in practice these are
#indicated on the command line
$incfiles = explode(",", "file1,file2"));

foreach($myarray as $cur) {
	foreach ($incfiles as $curfile) {
		require_once($curfile);

		#processElement is defined in each of the include files
		processElement($cur);
	}
}

Each of the include functions has a structure like this:

$myarray = array();
$myarray[] = "element1"
$myarray[] = "element2"

function processElement($item) {
	// Process the $item according to each $myarray element
	....
}

The problem is that after the LAST file to be included, overwrites the 
$myarray variable.  This variable needs to have the same name in order to 
make a consistent API.  Is there anyway to handle this, other than forcing a 
different name, or forcing the processElement routine into a separate process 
(i.e. call another script rather than include it..)?

If I continue with the current approach, I'll need the myarray variable to be 
reset each time through the inner loop (at the require_once() line...).  But 
seeing as the include file also defines some functions, I can't just include 
the file more than once... (duplicate function name errors then).

This is more of a code structure problem I guess, than a PHP how to type 
problem...

Thanks for any tips.  

Shawn




More information about the clug-progsig mailing list