2013-07-22 73 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