|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #78475 Unsetting or assigning a value to $GLOBALS makes it stop working
Submitted: 2019-08-29 18:14 UTC Modified: -
From: ayg at aryeh dot name Assigned:
Status: Open Package: Scripting Engine problem
PHP Version: 7.3.9 OS:
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Bug Type:
From: ayg at aryeh dot name
New email:
PHP Version: OS:


 [2019-08-29 18:14 UTC] ayg at aryeh dot name
$GLOBALS contains a reference to a magical array that has all the globals in it. Unsetting or assigning a value to it seems to forevermore lose the reference to this magical array, making $GLOBALS a regular old variable that has no connection to defined globals (although it's still a superglobal).

Unsetting or assigning to $GLOBALS probably should fail, just like $this does. I spent some time today trying to debug a scenario where $GLOBALS['someVar'] did not equal $someVar after I had written "global $someVar". It turned out to be because someone had unset $GLOBALS somewhere (they were trying to clear all global variables and restore them to an earlier state). This is really confusing and should be disallowed. Or at least produce a notice or something.

Test script:
$a = 1;
unset( $GLOBALS );
function f() {
  global $a;
  unset( $GLOBALS ); // $GLOBALS = []; is the same
  var_dump( $a );
  var_dump( isset( $GLOBALS['a' ) );

Expected result:




Actual result:


Add a Patch

Pull Requests

Add a Pull Request

PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Wed Oct 28 03:01:23 2020 UTC