473,836 Members | 2,068 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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($f ile_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 2324

"Deane Barker" <de***@slingand rock.com> wrote in message
news:3e******** *************** **@posting.goog le.com...
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($f ile_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.de mon.co.uk> wrote in message news:<cl******* ************@ne ws.demon.co.uk> ...
"Deane Barker" <de***@slingand rock.com> wrote in message
news:3e******** *************** **@posting.goog le.com...
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($f ile_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.goog le.com...
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.de mon.co.uk> wrote in message
news:<cl******* ************@ne ws.demon.co.uk> ...
"Deane Barker" <de***@slingand rock.com> wrote in message
news:3e******** *************** **@posting.goog le.com...
>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($f ile_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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
3393
by: Tom | last post by:
I'm tying myself in knots trying to figure out variable scope with constants and include files. This is what I'm doing: A page (index.php) on my website includes a general purpose include file (ini.inc) that declares a constant DAY_NUM. A little later in the file, ini.inc includes another file (b_data.inc) that tries to use DAY_NUM. I'm getting a "Use of Undefined Constant" notice as a result of this line when I load index.php in the...
134
7939
by: James A. Donald | last post by:
I am contemplating getting into Python, which is used by engineers I admire - google and Bram Cohen, but was horrified to read "no variable or argument declarations are necessary." Surely that means that if I misspell a variable name, my program will mysteriously fail to work with no error message. If you don't declare variables, you can inadvertently re-use an variable used in an enclosing context when you don't intend to, or
4
14909
by: Gery D. Dorazio | last post by:
Gurus, If a static variable is defined in a class what is the scope of the variable resolved to for it to remain 'static'? For instance, lets say I create a class library assembly that is strongly name which contains the class where the static variable is defined. This library can be referenced by multiple projects. I am fairly sure the static variable does not survive across the application boundry but does it within the application...
6
1485
by: Maarten | last post by:
Here is a nice problem. I have two scripts, one calling the other with via an include. The script that is included contains a variable, a function and a call to that function. The variable needs to be accessible to the function, so I declare it having a global scope. Then I execute the function, which should echo the value of the variable. Contrary to what I expected, the variable isn't there. The code of the 2 programs is: script1.php
23
19213
by: Russ Chinoy | last post by:
Hi, This may be a totally newbie question, but I'm stumped. If I have a function such as: function DoSomething(strVarName) { ..... }
18
33905
by: Jack | last post by:
Thanks.
1
25708
pbmods
by: pbmods | last post by:
VARIABLE SCOPE IN JAVASCRIPT LEVEL: BEGINNER/INTERMEDIATE (INTERMEDIATE STUFF IN ) PREREQS: VARIABLES First off, what the heck is 'scope' (the kind that doesn't help kill the germs that cause bad breath)? Scope describes the context in which a variable can be used. For example, if a variable's scope is a certain function, then that variable can only be used in that function. If you were to try to access that variable anywhere else in...
11
2930
by: whirlwindkevin | last post by:
I saw a program source code in which a variable is defined in a header file and that header file is included in 2 different C files.When i compile and link the files no error is being thrown.How is this possible.I thought it will throw "Variable redefinition Error". Giving the source code for reference.Please help me out on this... a.h ----- int g; void fun(void);
3
2049
by: SRoubtsov | last post by:
Dear all, Do you know whether ANSI C (or some other dialects) support the following: * a variable name coincides with a type name, * a structure/union field name coincides with a type name in the same file (.c + all relevant .h's)? e.g.
0
9820
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9670
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10844
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9374
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7792
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5826
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4452
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4016
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3115
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.