Jump to content

Including file Inside class


solepixel

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/123282-including-file-inside-class/
Share on other sites

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 :)

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.

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');

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");
          }
     }
     ... ... ...
}
?>

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.

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.