Take the following php class:
class test
{
function do_it_to_it(){
global $path;
if(somecondition){
$path = 'path/to/goodies';
require_once('some_file');
}
}
}
The contents of "some_file" are as follows:
global $path;
define('GOODIEPATH', $path);
Now I call:
$t1 = new test();
$t1->do_it_to_it();
$path in "some_file" will be null instead of "path/to/goodies." I have a feeling that this is because $path is either being overwritten for some reason by the second "global $path" or because of some type of global scope issue. What am I missing here?
P.S. - I hate globals more than anybody, but I can't fix it in this case without breaking legacy code.
EDIT: I have revised the test
class above to show an if
statement that is present in the real code. I seem to have resolved the issue by moving the global
statement inside the if
statement, like so:
class test
{
function do_it_to_it(){
if(somecondition){
global $path;
$path = 'path/to/goodies';
require_once('some_file');
}
}
}
Any idea why this seems to fix it? The if
statement causes the original global
declaration to be out of scope somehow?