Connecting Tech Pros Worldwide Forums | Help | Site Map

Variable Scope of Files Included Within Functions

Deane Barker
Guest
 
Posts: n/a
#1: Jul 17 '05
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

Tony Marston
Guest
 
Posts: n/a
#2: Jul 17 '05

re: Variable Scope of Files Included Within Functions



"Deane Barker" <deane@slingandrock.com> wrote in message
news:3ee9b2f.0410281851.4b27acb6@posting.google.co m...[color=blue]
>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.)[/color]

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



Phil Palmieri
Guest
 
Posts: n/a
#3: Jul 17 '05

re: Variable Scope of Files Included Within Functions


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" <tony@NOSPAM.demon.co.uk> wrote in message news:<clskdd$anl$1$8302bc10@news.demon.co.uk>...[color=blue]
> "Deane Barker" <deane@slingandrock.com> wrote in message
> news:3ee9b2f.0410281851.4b27acb6@posting.google.co m...[color=green]
> >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.)[/color]
>
> 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.[/color]
Deane Barker
Guest
 
Posts: n/a
#4: Jul 17 '05

re: Variable Scope of Files Included Within Functions


> Easy when you know how.

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

Thanks.

Deane
Tony Marston
Guest
 
Posts: n/a
#5: Jul 17 '05

re: Variable Scope of Files Included Within Functions


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" <google@page12.com> wrote in message
news:972d7ea.0410290437.79f9cda2@posting.google.co m...[color=blue]
> 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" <tony@NOSPAM.demon.co.uk> wrote in message
> news:<clskdd$anl$1$8302bc10@news.demon.co.uk>...[color=green]
>> "Deane Barker" <deane@slingandrock.com> wrote in message
>> news:3ee9b2f.0410281851.4b27acb6@posting.google.co m...[color=darkred]
>> >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.)[/color]
>>
>> 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.[/color][/color]


Closed Thread


Similar PHP bytes