这很有趣。首先,请注意,它似乎什么都没有做与静态方法:
$_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
表现相同。
我不完全确定这是为什么; somepages是指这两种结构的等价:
global $example;
$example =& $GLOBALS['example'];
这可能会导致在正确的轨道;不过,我希望你能用我的答案解决你的问题。
太棒了,谢谢!我曾尝试使用$ _GLOBALS,但我不确定为什么它在我尝试过时没有奏效。我现在唯一关心的就是我需要$ _APP来处理其他函数(尽管我不需要通过引用来分配)。我要检查它是否像我想要的那样工作。无论哪种方式,谢谢你的伟大答案! – GarbageGigo
对StackOverflow的回复不太熟悉,对多个评论感到抱歉。你认为我应该提交一个错误报告,因为我们不应该使用$ _GLOBALS?此外,谢谢你的链接(非常有助于理解参考)。 – GarbageGigo
我认为你需要一定的声誉来编辑你的评论。 :-)请确保使用'$ GLOBALS'而不是'$ _GLOBALS'(但我猜这是一个错字)。至于提交bug,我不确定,我认为这种行为可能是PHP本身的实现的效果,或者更确切地说,是因为您应该认为更多的人应该在过去遇到它。也许这是有道理的,尽管我看不出为什么到目前为止。也许你可以立即与PHP开发团队的人取得联系,而无需立即提交错误消息?不知道什么是最好的方法。 – stef77