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