469,610 Members | 1,774 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,610 developers. It's quick & easy.

Variable Scope of Files Included Within Functions

I have a function that selects a file to include, then includes is.
The file is including within the function, like so:

function include_file($file_name)
{
[lots of logic here to figure out the path...]
require $file_name;
return;
}

My problem is that the file gets included *within* the function, so
all the code in the file inherits the function's scope. Any variables
declared in the include file are not global -- they're local to that
function.

Is there any way around this? Is there anything I can put in the
included file or in the function to make sure that variables declared
within it are global? (I can't just reference the variables before
the require, because I have no way of knowing what's in the include
file.)

Deane
Jul 17 '05 #1
4 2102

"Deane Barker" <de***@slingandrock.com> wrote in message
news:3e*************************@posting.google.co m...
I have a function that selects a file to include, then includes is.
The file is including within the function, like so:

function include_file($file_name)
{
[lots of logic here to figure out the path...]
require $file_name;
return;
}

My problem is that the file gets included *within* the function, so
all the code in the file inherits the function's scope. Any variables
declared in the include file are not global -- they're local to that
function.

Is there any way around this? Is there anything I can put in the
included file or in the function to make sure that variables declared
within it are global? (I can't just reference the variables before
the require, because I have no way of knowing what's in the include
file.)


Use the function to determine the filename, but instead of include()ing it
within the function just return the name to the calling script. When the
calling script issues the include() any variables will then be within the
scope of that script, not the function.

Easy when you know how.

--
Tony Marston

http://www.tonymarston.net

Jul 17 '05 #2
This is also a messy solution, but it does work.. Inside your
function set the 'global' variable to a session ie:

function some_function(){
blah blah blah

$_SESSION[some_function] = $the_global_var;
}

Phil

"Tony Marston" <to**@NOSPAM.demon.co.uk> wrote in message news:<cl*******************@news.demon.co.uk>...
"Deane Barker" <de***@slingandrock.com> wrote in message
news:3e*************************@posting.google.co m...
I have a function that selects a file to include, then includes is.
The file is including within the function, like so:

function include_file($file_name)
{
[lots of logic here to figure out the path...]
require $file_name;
return;
}

My problem is that the file gets included *within* the function, so
all the code in the file inherits the function's scope. Any variables
declared in the include file are not global -- they're local to that
function.

Is there any way around this? Is there anything I can put in the
included file or in the function to make sure that variables declared
within it are global? (I can't just reference the variables before
the require, because I have no way of knowing what's in the include
file.)


Use the function to determine the filename, but instead of include()ing it
within the function just return the name to the calling script. When the
calling script issues the include() any variables will then be within the
scope of that script, not the function.

Easy when you know how.

Jul 17 '05 #3
> Easy when you know how.

[sigh] Why is it so easy to overlook the most obvious solution to a problem?

Thanks.

Deane
Jul 17 '05 #4
If you look at the OP's question he does not say that he wants the variables
inside the function to be loaded into the $_SESSION array, therefore your
answer is not appropriate.

--
Tony Marston

http://www.tonymarston.net

"Phil Palmieri" <go****@page12.com> wrote in message
news:97*************************@posting.google.co m...
This is also a messy solution, but it does work.. Inside your
function set the 'global' variable to a session ie:

function some_function(){
blah blah blah

$_SESSION[some_function] = $the_global_var;
}

Phil

"Tony Marston" <to**@NOSPAM.demon.co.uk> wrote in message
news:<cl*******************@news.demon.co.uk>...
"Deane Barker" <de***@slingandrock.com> wrote in message
news:3e*************************@posting.google.co m...
>I have a function that selects a file to include, then includes is.
> The file is including within the function, like so:
>
> function include_file($file_name)
> {
> [lots of logic here to figure out the path...]
> require $file_name;
> return;
> }
>
> My problem is that the file gets included *within* the function, so
> all the code in the file inherits the function's scope. Any variables
> declared in the include file are not global -- they're local to that
> function.
>
> Is there any way around this? Is there anything I can put in the
> included file or in the function to make sure that variables declared
> within it are global? (I can't just reference the variables before
> the require, because I have no way of knowing what's in the include
> file.)


Use the function to determine the filename, but instead of include()ing
it
within the function just return the name to the calling script. When the
calling script issues the include() any variables will then be within the
scope of that script, not the function.

Easy when you know how.

Jul 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Tom | last post: by
134 posts views Thread by James A. Donald | last post: by
4 posts views Thread by Gery D. Dorazio | last post: by
6 posts views Thread by Maarten | last post: by
23 posts views Thread by Russ Chinoy | last post: by
18 posts views Thread by Jack | last post: by
pbmods
1 post views Thread by pbmods | last post: by
11 posts views Thread by whirlwindkevin | last post: by
3 posts views Thread by SRoubtsov | last post: by
reply views Thread by devrayhaan | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.