2013-07-22 43 views
3

我在PHP中遇到全局变量的问题。我的问题是,我在静态类方法内改变的全局变量没有在方法外部更新。全局变量不在PHP中的静态类方法中更新

我已经包括了代码:

test.php的

define('APP_ID', 'TESTING'); 
$_APP = array('test' => 'test value'); 
include ('appsettings.class.php'); 
AppSettings::initApplication(); 

appsettings.class.php

class AppSettings 
{ 
    public static function initApplication() 
    { 
    global $_APP; 
    session_start(); 

    // Some code here for your initializtions 
    self::initAppEngine(); 
echo '<pre>Inside initApplication: '; print_r($_APP); 
echo '<pre>Directly printing the session variable: '; print_r($_SESSION[APP_ID]); 
    } 

    private static function initAppEngine() 
    { 
    global $_APP; 

    if(isset($_SESSION[APP_ID])) 
    { 
     $_APP = &$_SESSION[APP_ID]; 
    } 
    else 
    { 
     $_SESSION[APP_ID] = array('abcd' => 'hello', 'APP_ID' => APP_ID); 
     $_APP = &$_SESSION[APP_ID]; 
die("Refresh the page"); 
    } 

    if (!isset($_APP['uid'])) 
     $_APP['uid'] = 0; 

echo '<pre>Inside initAppEngine: '; print_r($_APP); 
    } 
} 

$ _APP的旧值即将到来的新的代替在initApplication里面。任何人都可以指出我做错了什么?

在此先感谢,

回答

3

这很有趣。首先,请注意,它似乎什么都没有做与静态方法:

$_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']; 

这可能会导致在正确的轨道;不过,我希望你能用我的答案解决你的问题。

+0

太棒了,谢谢!我曾尝试使用$ _GLOBALS,但我不确定为什么它在我尝试过时没有奏效。我现在唯一关心的就是我需要$ _APP来处理其他函数(尽管我不需要通过引用来分配)。我要检查它是否像我想要的那样工作。无论哪种方式,谢谢你的伟大答案! – GarbageGigo

+0

对StackOverflow的回复不太熟悉,对多个评论感到抱歉。你认为我应该提交一个错误报告,因为我们不应该使用$ _GLOBALS?此外,谢谢你的链接(非常有助于理解参考)。 – GarbageGigo

+0

我认为你需要一定的声誉来编辑你的评论。 :-)请确保使用'$ GLOBALS'而不是'$ _GLOBALS'(但我猜这是一个错字)。至于提交bug,我不确定,我认为这种行为可能是PHP本身的实现的效果,或者更确切地说,是因为您应该认为更多的人应该在过去遇到它。也许这是有道理的,尽管我看不出为什么到目前为止。也许你可以立即与PHP开发团队的人取得联系,而无需立即提交错误消息?不知道什么是最好的方法。 – stef77