solepixel Posted September 8, 2008 Share Posted September 8, 2008 I'm trying to setup a class that will allow a file to be included on the page. It goes something like: <?php class myClass { ... ... ... function includeFile(){ if(file_exists($this->include_file)){ include($this->include_file); } else { $this->error("file missing"); } } ... ... ... } $foo = "bar"; $page = new myClass(); $page->include_file = "the_file.php"; $page->includeFile(); ?> // the_file.php <?php echo $foo; ?> It has something to do with variable scope and accessing globals or something, I'm not sure. My question is how should I setup my class so that I do not have to use "return" at the bottom of any files I want to include with my class? I want the includes to function just like normal includes. I tried <?php class myClass { ... ... ... function includeFile(){ global $GLOBALS; global $HTTP_SERVER_VARS; if(file_exists($this->include_file)){ include($this->include_file); } else { $this->error("file missing"); } } ... ... ... } but that still doesn't make $foo print "bar". Thanks for any help on how to accomplish this. Quote Link to comment Share on other sites More sharing options...
discomatt Posted September 8, 2008 Share Posted September 8, 2008 Think more object-oriented When a file is included, the code it contains inherits the variable scope of the line on which the include occurs. Any variables available at that line in the calling file will be available within the called file, from that point forward. However, all functions and classes defined in the included file have the global scope. Rather than 'include' a file with a variable, include a file with a function or a class Quote Link to comment Share on other sites More sharing options...
solepixel Posted September 8, 2008 Author Share Posted September 8, 2008 Not sure what you mean, can you please elaborate? Quote Link to comment Share on other sites More sharing options...
solepixel Posted September 8, 2008 Author Share Posted September 8, 2008 Sorry, I think I know what you are trying to tell me know. But I want the idea of the include file to function exactly like a normal include file on a page. This class is being written with the common user in mind. I want to avoid having any special code inside any include files, if at all possible. Quote Link to comment Share on other sites More sharing options...
discomatt Posted September 8, 2008 Share Posted September 8, 2008 Then you're going to have to deal with scope. If you want to use a non-OOP methodology with an OOP one you're gonna have to hack it together. You'll have to make your includeFile() a global function, and replace $page = new myClass(); $page->include_file = "the_file.php"; $page->includeFile(); with includeFile('the_file.php'); Quote Link to comment Share on other sites More sharing options...
solepixel Posted September 8, 2008 Author Share Posted September 8, 2008 Is there no way to pull all global variables inside that function? Quote Link to comment Share on other sites More sharing options...
solepixel Posted September 8, 2008 Author Share Posted September 8, 2008 Ok, I think I found the answer here: http://www.tellinya.com/read/2007/08/21/52.html with the following code to put globals inside my function: <?php reset($GLOBALS); // Always reset Arrays before parsed with while or foreach while(list($key,$val)=each($GLOBALS)){ if(($key==strstr($key,"HTTP_")) || (strstr($key,"_")==$key)) continue; eval("global $".$key.";"); } reset($GLOBALS); ?> making this my final function/class: <?php class myClass { ... ... ... function includeFile(){ reset($GLOBALS); // Always reset Arrays before parsed with while or foreach while(list($key,$val)=each($GLOBALS)){ if(($key==strstr($key,"HTTP_")) || (strstr($key,"_")==$key)) continue; eval("global $".$key.";"); } reset($GLOBALS); if(file_exists($this->include_file)){ include($this->include_file); } else { $this->error("file missing"); } } ... ... ... } ?> Quote Link to comment Share on other sites More sharing options...
solepixel Posted September 8, 2008 Author Share Posted September 8, 2008 Actually, i kind of like this better global ${$key}; instead of this: eval("global $".$key.";"); It makes more sense to me to do it that way. Seems a bit safer too, i suppose. Quote Link to comment Share on other sites More sharing options...
discomatt Posted September 8, 2008 Share Posted September 8, 2008 It really depends. What you're trying to do isn't considered 'good programming.' You have the potential of over writing previously defined variables... Can I see an example file you're including? Are the files you're including going to have a variable naming pattern? There is a 'global' keyword you can use in functions to force global scope. Quote Link to comment Share on other sites More sharing options...
solepixel Posted September 8, 2008 Author Share Posted September 8, 2008 Actually, I think i found an even better solution by just adding this: extract($GLOBALS); to the top of my function. The page I'm using on is sort of an application template. It allows me to drop a set of files on a server, then upload styles, image, and the main XHTML, and it I get started working on the application very quickly. It also works with websites as well. There's not much code except the part that takes the URL and figures out which page in the database to pull. if the page is an "include" than it gets the include file. The next step is figure out how to get any newly created variables in the included file into the global scope, again without changing the way the variables are written in the include file. Quote Link to comment Share on other sites More sharing options...
discomatt Posted September 8, 2008 Share Posted September 8, 2008 Okay, but I warn you, generic massive changes to the global scope can cause errors that are extremely hard to debug. In my opinion, this is a bad solution. Keep a VERY watchful eye on generic variable names. Quote Link to comment Share on other sites More sharing options...
solepixel Posted September 8, 2008 Author Share Posted September 8, 2008 Well, i think for now, I will turn a solution where I put global $vars at the top of my include files whenever accessing these variables is necessary. Thanks for the help. Quote Link to comment Share on other sites More sharing options...
DarkWater Posted September 8, 2008 Share Posted September 8, 2008 Or, you just pass the stuff around when you need it into the function... Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.