全局变量不在PHP中的静态类方法内更新

时间:2013-07-22 10:13:16

标签: php global-variables superglobals

我在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中而不是新值。谁能指出我做错了什么?

提前致谢,

1 个答案:

答案 0 :(得分: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']语法而不能使用{{1 }}。如果您不需要通过引用分配,global $_APP$GLOBALS['_APP']的行为相同。

我不确定为什么会这样; some pages指的是这两种结构的等价性:

global $_APP

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