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
Global variables in functions from included files.
Moderator: General Moderators
i dont know specifically about writing this sort of thing (I'm an OO developer so when I talk being modular I mean abstracting everything behind object interfaces) - but a problem I had was that the preprocessor is veyr fussy about the order in which it compiles your source code ..
if I was trying to inherit from a base class which had an include file listed AFTER the one that was trying to inherit from it it wouldnt compile ..
I am guessing its the same kind of thing .. if you want to reference a global in an include file make sure that its declared prior to the include_once state for any code that references it ..
btw .. I am just guessing based on my experience of getting OO PHP working ..
best of luck all the same ..
Ant
if I was trying to inherit from a base class which had an include file listed AFTER the one that was trying to inherit from it it wouldnt compile ..
I am guessing its the same kind of thing .. if you want to reference a global in an include file make sure that its declared prior to the include_once state for any code that references it ..
btw .. I am just guessing based on my experience of getting OO PHP working ..
best of luck all the same ..
Ant
Thanks for the reply.
I beleive ive narrowed down the problem however. It seems as if includes within includes use the include path of the orginal included file
so if the orginal included file is
index.php
and you have the following directories
functions, configuration, misc
and you do:
include_once("functions/function.php");
and then in functions.php you do:
include_once("../configuration/globalconfig.php");
it will fail because the include path is relitive to the first included file. however for some reason if you fix it and do
include_once("configuration/globalconfig.php");
it will work, but it will be BROKEN in that the variables decalred in globalconfig will not be accessible inside functions in the included file with the "modified path"
so if i do include_once("configuration/globalconfig.php");, which is the only way i can get it to work.. the variables won't be avialable inside functions within this path.. its so annyoing, and for some reason they are available outside functions.. actually if you do this NO global variables, not even ones defined in the confbalconfig.php will be accessible inside functions..
this is stressful, i now feel that is this a bug of sorts..
i was thinking of a work around by either moving all the files into one directory (messy as hell
and i need organization in my files)
or perhaps setting PHP's include path to the PATH of those files.. however since this script is meant to be installed in various directories, I am having trouble doing this..
id need to get the name of the directory the index.php file resides in, and then add the names of the directories inside that path to the global path.. which means to keep it clean, i have to create an initilizatoin function.. bleh bleh bleh
anyone know how to do this and post a fix? im gonna be digging in the manual for awhile, but if someone could post a way to update the PHP path in the script pointing to the path that an index.php (main php file) resides in id be greatful..
thanks,
nathan
I beleive ive narrowed down the problem however. It seems as if includes within includes use the include path of the orginal included file
so if the orginal included file is
index.php
and you have the following directories
functions, configuration, misc
and you do:
include_once("functions/function.php");
and then in functions.php you do:
include_once("../configuration/globalconfig.php");
it will fail because the include path is relitive to the first included file. however for some reason if you fix it and do
include_once("configuration/globalconfig.php");
it will work, but it will be BROKEN in that the variables decalred in globalconfig will not be accessible inside functions in the included file with the "modified path"
so if i do include_once("configuration/globalconfig.php");, which is the only way i can get it to work.. the variables won't be avialable inside functions within this path.. its so annyoing, and for some reason they are available outside functions.. actually if you do this NO global variables, not even ones defined in the confbalconfig.php will be accessible inside functions..
this is stressful, i now feel that is this a bug of sorts..
i was thinking of a work around by either moving all the files into one directory (messy as hell
or perhaps setting PHP's include path to the PATH of those files.. however since this script is meant to be installed in various directories, I am having trouble doing this..
id need to get the name of the directory the index.php file resides in, and then add the names of the directories inside that path to the global path.. which means to keep it clean, i have to create an initilizatoin function.. bleh bleh bleh
anyone know how to do this and post a fix? im gonna be digging in the manual for awhile, but if someone could post a way to update the PHP path in the script pointing to the path that an index.php (main php file) resides in id be greatful..
thanks,
nathan
Thanks for the reply.
I beleive ive narrowed down the problem however. It seems as if includes within includes use the include path of the orginal included file
so if the orginal included file is
index.php
and you have the following directories
functions, configuration, misc
and you do:
include_once("functions/function.php");
and then in functions.php you do:
include_once("../configuration/globalconfig.php");
it will fail because the include path is relitive to the first included file. however for some reason if you fix it and do
include_once("configuration/globalconfig.php");
it will work, but it will be BROKEN in that the variables decalred in globalconfig will not be accessible inside functions in the included file with the "modified path"
so if i do include_once("configuration/globalconfig.php");, which is the only way i can get it to work.. the variables won't be avialable inside functions within this path.. its so annyoing, and for some reason they are available outside functions.. actually if you do this NO global variables, not even ones defined in the confbalconfig.php will be accessible inside functions..
this is stressful, i now feel that is this a bug of sorts..
i was thinking of a work around by either moving all the files into one directory (messy as hell
and i need organization in my files)
or perhaps setting PHP's include path to the PATH of those files.. however since this script is meant to be installed in various directories, I am having trouble doing this..
id need to get the name of the directory the index.php file resides in, and then add the names of the directories inside that path to the global path.. which means to keep it clean, i have to create an initilizatoin function.. bleh bleh bleh
anyone know how to do this and post a fix? im gonna be digging in the manual for awhile, but if someone could post a way to update the PHP path in the script pointing to the path that an index.php (main php file) resides in id be greatful..
thanks,
nathan
I beleive ive narrowed down the problem however. It seems as if includes within includes use the include path of the orginal included file
so if the orginal included file is
index.php
and you have the following directories
functions, configuration, misc
and you do:
include_once("functions/function.php");
and then in functions.php you do:
include_once("../configuration/globalconfig.php");
it will fail because the include path is relitive to the first included file. however for some reason if you fix it and do
include_once("configuration/globalconfig.php");
it will work, but it will be BROKEN in that the variables decalred in globalconfig will not be accessible inside functions in the included file with the "modified path"
so if i do include_once("configuration/globalconfig.php");, which is the only way i can get it to work.. the variables won't be avialable inside functions within this path.. its so annyoing, and for some reason they are available outside functions.. actually if you do this NO global variables, not even ones defined in the confbalconfig.php will be accessible inside functions..
this is stressful, i now feel that is this a bug of sorts..
i was thinking of a work around by either moving all the files into one directory (messy as hell
or perhaps setting PHP's include path to the PATH of those files.. however since this script is meant to be installed in various directories, I am having trouble doing this..
id need to get the name of the directory the index.php file resides in, and then add the names of the directories inside that path to the global path.. which means to keep it clean, i have to create an initilizatoin function.. bleh bleh bleh
anyone know how to do this and post a fix? im gonna be digging in the manual for awhile, but if someone could post a way to update the PHP path in the script pointing to the path that an index.php (main php file) resides in id be greatful..
thanks,
nathan