编程语言
首页 > 编程语言> > 全局变量未在PHP的静态Class方法内部更新

全局变量未在PHP的静态Class方法内部更新

作者:互联网

我在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中的新值.谁能指出我做错了什么?

提前致谢,

解决方法:

这很有趣.首先,请注意,它似乎与静态方法无关:

$_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’]语法,而不能使用全局$_APP.如果不需要通过引用进行分配,则$GLOBALS [‘_ APP’]和全局$_APP的行为相同.

我不确定为什么会这样. some pages指的是这两个构造的等效性:

global $example;
$example =& $GLOBALS['example'];

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

标签:superglobals,global-variables,php
来源: https://codeday.me/bug/20191123/2064251.html