I am trying to define a function that is *like* the standard PHP "include()" function but is slightly different, but I am running into trouble with varible scoping. The idea is this:
I want to mimic "include()" in a way such that when I include a file, it can then include files relative to itself, instead of relative to the root file in the chain of includes. For example, if I do something like this: // we are here: /root/file.php include("subDir/anotherFile.php"); // we are here: /root/subDir/anotherFile.php include("diffSubDir/diffFile.php"); I get a warning saying that "diffSubDir" is not a valid directory, because it is actually looking for "/root/diffSubDir/diffFile.php" when I mean to include "/root/subDir/diffSubDir/diffFile.php". The first thing I did was this: $myPath = getcwd()."/"; chdir($myPath."subDir/"); include("anotherFile.php"); chdir($myPath); That's fine, but it's kind of kludgey, because I need to be really careful not to do crush my $rootPath variable if I need to do it again inside my include... So the next step was to protect the variable by making it local to a function. Here is what I got: function myInclude($fileName) { //you are here $rootPath = getcwd()."/"; //find where we need to go $aryFilePath = split("/", $fileName); $fileToInclude = array_pop($aryFilePath); $includePath = join("/", $aryFilePath) . "/"; //do the include chdir($rootPath.$includePath); include($fileToInclude); chdir($rootPath); } So that's great! It does almost everything I want it to do, EXCEPT: the variable scope within the includes is screwy. The file included in this manner is local to the function, so it does not have inherent access to the variables set before/after the call to myInclude(). That makes sense conceptually, but it doesn't solve my problem. Declaring variables as "global" doesn't really fix it because a) I want to keep this generic, so I won't always know what variables to call as global, and b) if I call this function from within an object, I want to keep my variables local to the class, but not to the function call within the class. If I am in the class, for example, and I declare a var as global, I just lost my encapsulation. Yuck. What I really want is for this function to work just like the native PHP "include()" function, where it does its thing without making its own scope. Is there any way to do such a thing? I tried declaring and calling the function as &myInclude(), but that didn't do it either. Am I out-of-luck, or is there some cool trick I haven't learned yet? Thanks, David Huyck -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php