Re: global vars inside includes inside functions?

From: Date: Thu, 29 Apr 2004 05:52:23 +0000
Subject: Re: global vars inside includes inside functions?
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-184924@lists.php.net to get a copy of this message
Hi, Thursday, April 29, 2004, 2:27:39 PM, you wrote: JF> Can someone see a way to achieve what I want? JF> I have the following function: JF> <? JF> function safeInclude($file) { JF> if(file_exists($file)) { JF> include($file); JF> } else { JF> debug("file $file not found"); JF> } JF> } ?>> JF> When used in this context... JF> <? JF> $something = 'foo'; JF> safeInclude('somethingElse.inc'); ?>> JF> ... somethingElse.inc cannot see the global variable $something. JF> So, if an include is wrapped in a function, it would appear that the JF> global variables no longer apply to the namespace of the included file. JF> Is there anyway I can get around this? JF> My only guess is I could add the line "global $GLOBALS;" to JF> safeInclude(), but I have no idea how this affects memory usage or JF> anything else. JF> --- JF> Justin French JF> http://indent.com.au you would probably have to do something like function safeInclude($file) { eval('global '.('$'.implode(',$',array_keys($GLOBALS))).';'); . . . } ugly but should work -- regards, Tom

« previous php.general (#184924) next »