Re: global vars inside includes inside functions?
| From: | De Greef S�bastien | Date: | Thu, 29 Apr 2004 08:45:11 +0000 |
| Subject: | Re: global vars inside includes inside functions? | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-184930@lists.php.net to get a copy of this message | ||
normally you just put
global $something;
at the begining of your function..
Like this
function safeInclude($file) {
global $something;
if(file_exists($file)) { include($file);
} else {
debug("file $file not found");
}
}
"Justin French" <justin@indent.com.au> a écrit dans le message news:
8CC3F80A-9995-11D8-A430-000A9579CE3A@indent.com.au...
> Can someone see a way to achieve what I want?
>
> I have the following function:
>
> <?
> function safeInclude($file) {
> if(file_exists($file)) {
> include($file);
> } else {
> debug("file $file not found");
> }
> }
> ?>
>
> When used in this context...
>
> <?
> $something = 'foo';
> safeInclude('somethingElse.inc');
> ?>
>
> ... somethingElse.inc cannot see the global variable $something.
>
>
> So, if an include is wrapped in a function, it would appear that the
> global variables no longer apply to the namespace of the included file.
>
> Is there anyway I can get around this?
>
> My only guess is I could add the line "global $GLOBALS;" to
> safeInclude(), but I have no idea how this affects memory usage or
> anything else.
>
>
>
> ---
> Justin French
> http://indent.com.au