Global variables in functions from included files.
Posted: Sun Nov 10, 2002 5:05 pm
i am having trouble with global variables that are in other files..
i am trying to abstract everything, and have a global configuration file, which is used to hold configuration details about a modular web application i am trying to write.
heres where the problem lies..
the index.php includes php scripts(functions actually), and the scripts included inturn include more scripts, and what not. I am using include_once to avoid any messes..
now when i use a function in an include script (2 levels down the chain, a function that exists in an included file, of an included file), it will not let me access global variables with the global keyword
eg
index.php
<?
include_once("content.php");
show_main_content();
test_Function() ; // from functions.php, included in the included file.
?>
----------------
content.php
<?
include_once("functions.php");
$this_variable = "test_var";
show_main_content() {
global $this_variable;
echo $this_variable; // works
}
?>
--------------------
functions.php
<?
echo $this_variable; // WORKS FINE
function test_Function() {
global $this_variable;
echo $this_variable; // DOESNT WORK
}
?>
this is similar in the way im trying to use this in my application. ive even tried $GLOBALS['bleh'], but it doesnt show up in the $GLOBALS array in the function. A quick and dirty solution is to pass it to the function, but Im trying to create a modular specification for addons to this application, so this simply won't do..
eg. I want to see a configruation file that configures the WHOLE SHABANG (the whole site, including modules) to use these database functions to conect and disconnect from a database.. and there for have a configuration file containing global variables such as $DBSERVERIP.
Ive searched the archeive and can't find anything to help.. Ive had no problems using globals in the past, but seems that including files screws up the globals array inside functions.. is this a bug? is there away around it? am i missing somehting?
thanks alot,
nathan
i am trying to abstract everything, and have a global configuration file, which is used to hold configuration details about a modular web application i am trying to write.
heres where the problem lies..
the index.php includes php scripts(functions actually), and the scripts included inturn include more scripts, and what not. I am using include_once to avoid any messes..
now when i use a function in an include script (2 levels down the chain, a function that exists in an included file, of an included file), it will not let me access global variables with the global keyword
eg
index.php
<?
include_once("content.php");
show_main_content();
test_Function() ; // from functions.php, included in the included file.
?>
----------------
content.php
<?
include_once("functions.php");
$this_variable = "test_var";
show_main_content() {
global $this_variable;
echo $this_variable; // works
}
?>
--------------------
functions.php
<?
echo $this_variable; // WORKS FINE
function test_Function() {
global $this_variable;
echo $this_variable; // DOESNT WORK
}
?>
this is similar in the way im trying to use this in my application. ive even tried $GLOBALS['bleh'], but it doesnt show up in the $GLOBALS array in the function. A quick and dirty solution is to pass it to the function, but Im trying to create a modular specification for addons to this application, so this simply won't do..
eg. I want to see a configruation file that configures the WHOLE SHABANG (the whole site, including modules) to use these database functions to conect and disconnect from a database.. and there for have a configuration file containing global variables such as $DBSERVERIP.
Ive searched the archeive and can't find anything to help.. Ive had no problems using globals in the past, but seems that including files screws up the globals array inside functions.. is this a bug? is there away around it? am i missing somehting?
thanks alot,
nathan