Re: include/require inside of function

From: Date: Fri, 04 Jul 2003 07:28:49 +0000
Subject: Re: include/require inside of function
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-153968@lists.php.net to get a copy of this message
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

« previous php.general (#153968) next »