Hi,

If the file you are including is of your own authorage (I know that isn't a word, but whatever :), just refer to global variables always as an index of the $GLOBALS array. This is good practice anyways for any file that might be included by another user, for exactly this issue.

I have a file that could be a global include or not in my project, and making sure every global variable is referenced as $GLOBALS['varname'] fixed it for me without any fancy code (although Rasmus's code is very nice)

Regards,
Greg
--
phpDocumentor
http://www.phpdoc.org

Rasmus Lerdorf wrote:
On Fri, 4 Jul 2003, Aric Caley wrote:

Is there anyway to include a file inside of a function and have the included
stuff be global?  For instance if I defined a class or a function in that
include file, I want to be able to use that class outside of the function.

On the documentation for include() a poster commented that it did indeed
work like this, but my testing indicates it does not.  Everything stays
local to the function and goes away when the function ends.

Is there a way?


Functions defined in included files are always global.  So I guess it is
just the variable you want to put out into the global symbol table.  It's
a little bit tricky, but you can do it like this:

    function foo($filename) {
        extract($GLOBALS, EXTR_REFS);
        include $filename;
        $arr = array_diff(get_defined_vars(),$GLOBALS);
        foreach($arr as $var=>$val) $GLOBALS[$var] = $val;
    }

-Rasmus



-- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php



Reply via email to