This is quite interesting. First of all, note that it seems to have nothing to do with static methods:
$_SESSION['test'] = array("test value from superglobal");
$_APP = array('test' => "test value directly assigned");
class AppSettings
{
public static function initApplication()
{
global $_APP;
$_APP = &$_SESSION['test'];
echo '<pre>Inside initApplication: '; print_r($_APP);
}
public function initApplicationNonStatic()
{
global $_APP;
$_APP = &$_SESSION['test'];
echo '<pre>Inside initApplicationNonStatic: '; print_r($_APP);
}
}
echo '<pre>Before calling initApplication: '; print_r($_APP);
AppSettings::initApplication();
echo '<pre>After calling initApplication: '; print_r($_APP);
echo '<pre>Before calling initApplicationNonStatic: '; print_r($_APP);
$appSettings = new AppSettings();
$appSettings->initApplicationNonStatic();
echo '<pre>After calling initApplicationNonStatic: '; print_r($_APP);
Result:
Before calling initApplication: Array
(
[test] => test value directly assigned
)
Inside initApplication: Array
(
[0] => test value from superglobal
)
After calling initApplication: Array
(
[test] => test value directly assigned
)
Before calling initApplicationNonStatic: Array
(
[test] => test value directly assigned
)
Inside initApplicationNonStatic: Array
(
[0] => test value from superglobal
)
After calling initApplicationNonStatic: Array
(
[test] => test value directly assigned
)
But this works:
$_SESSION['test'] = array("test value from superglobal");
$_APP = array('test' => "test value directly assigned");
class AppSettings2
{
public function initApplicationNonStatic()
{
$GLOBALS['_APP'] = &$_SESSION['test']; // by reference
echo '<pre>Inside initApplicationNonStatic: '; print_r($GLOBALS['_APP']);
}
}
echo '<pre>Before calling initApplicationNonStatic: '; print_r($_APP);
$appSettings2 = new AppSettings2();
$appSettings2->initApplicationNonStatic();
echo '<pre>After calling initApplicationNonStatic: '; print_r($_APP);
$_SESSION['test'] = array("test value from superglobal altered");
echo '<pre>After altering superglobal: '; print_r($_APP);
Result:
Before calling initApplicationNonStatic: Array
(
[test] => test value directly assigned
)
Inside initApplicationNonStatic: Array
(
[0] => test value from superglobal
)
After calling initApplicationNonStatic: Array
(
[0] => test value from superglobal
)
After altering superglobal: Array
(
[0] => test value from superglobal altered
)
And this works, too:
$_SESSION['test'] = array("test value from superglobal");
$_APP = array('test' => "test value directly assigned");
class AppSettings2
{
public function initApplicationNonStatic()
{
global $_APP;
$_APP = $_SESSION['test']; // by value
echo '<pre>Inside initApplicationNonStatic: '; print_r($_APP);
}
}
echo '<pre>Before calling initApplicationNonStatic: '; print_r($_APP);
$appSettings2 = new AppSettings2();
$appSettings2->initApplicationNonStatic();
echo '<pre>After calling initApplicationNonStatic: '; print_r($_APP);
$_SESSION['test'] = array("test value from superglobal altered");
echo '<pre>After altering superglobal: '; print_r($_APP);
Result:
Before calling initApplicationNonStatic: Array
(
[test] => test value directly assigned
)
Inside initApplicationNonStatic: Array
(
[0] => test value from superglobal
)
After calling initApplicationNonStatic: Array
(
[0] => test value from superglobal
)
After altering superglobal: Array
(
[0] => test value from superglobal // expected, since assigned by value
)
So, it seems that whenever you want to assign a reference to a global variable inside a function or method, you have to use the $GLOBALS['_APP']
syntax and you cannot use global $_APP
. If you don't need the assignment by reference, $GLOBALS['_APP']
and global $_APP
behave the same.
I'm not exactly sure why this is so; some pages refer to the equivalence of these two constructs:
global $example;
$example =& $GLOBALS['example'];
This might lead to the right track; however, I hope that you can troubleshoot your problem with my answer.