Connecting Tech Pros Worldwide Help | Site Map

Variable Scope of Files Included Within Functions

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 17th, 2005, 09:21 AM
Deane Barker
Guest
 
Posts: n/a
Default 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

  #2  
Old July 17th, 2005, 09:21 AM
Tony Marston
Guest
 
Posts: n/a
Default 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



  #3  
Old July 17th, 2005, 09:21 AM
Phil Palmieri
Guest
 
Posts: n/a
Default 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]
  #4  
Old July 17th, 2005, 09:21 AM
Deane Barker
Guest
 
Posts: n/a
Default 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
  #5  
Old July 17th, 2005, 09:21 AM
Tony Marston
Guest
 
Posts: n/a
Default 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]


 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.