ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

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

2019-11-23 02:32:35  阅读:265  来源: 互联网

标签:superglobals global-variables 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中的新值.谁能指出我做错了什么?

提前致谢,

解决方法:

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

$_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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有