2013-07-22 4 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'을 사용해야합니다. (그러나 나는 오타라고 생각합니다).버그를 제기하는 것에 관해서는 잘 모르겠다. 나는이 행동이 의도 된 것이거나 PHP 자체의 구현이 효과적이라고 생각한다. 특히 과거에 더 많은 사람들이 그것을 실행해야한다고 생각해야하기 때문이다. 아마도 지금까지는 그 이유를 알 수는 없지만 아마도 의미가 있습니다. 아마 당신은 버그를 바로 제기하지 않고 PHP 개발자 팀의 누군가와 접촉 할 수 있습니까? 가장 좋은 방법이 무엇인지 모릅니다. – stef77