这很有趣。首先请注意,它似乎与静态方法无关:
$_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);
结果:
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
)
但这有效:
$_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);
结果:
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
)
这也有效:
$_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);
结果:
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
)
因此,似乎每当您想在函数或方法中分配对全局变量的引用时,都必须使用$GLOBALS['_APP']
语法,而不能使用global $_APP
. 如果您不需要通过引用进行分配,$GLOBALS['_APP']
并且global $_APP
行为相同。
我不确定为什么会这样;一些 页面提到了这两个结构的等价性:
global $example;
$example =& $GLOBALS['example'];
这可能会导致正确的轨道;但是,我希望您可以通过我的回答解决您的问题。