php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #24910 in function scope, $GLOBALS is not superglobal
Submitted: 2003-08-01 14:47 UTC Modified: 2003-08-01 20:26 UTC
From: greg at chiaraquartet dot net Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 4.3.2 OS: Windows XP
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.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: greg at chiaraquartet dot net
New email:
PHP Version: OS:

 

 [2003-08-01 14:47 UTC] greg at chiaraquartet dot net
Description:
------------
the $GLOBALS variables contains indexes to every superglobal except 'GLOBALS' in function scope.  In global scope, it contains the 'GLOBALS' index as a self-reference

Reproduce code:
---------------
<?php 
function blah() {
  var_dump($GLOBALS['GLOBALS']);

}
blah();
?>

Expected result:
----------------
var_dump() of the $GLOBALS superglobal

Actual result:
--------------
Notice: Undefined index: GLOBALS in c:\web pages\chiara\a1.php on line 3
NULL

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-08-01 15:23 UTC] iliaa@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

$GLOBALS['GLOBALS'] is actually a reference to $GLOBALS if you want to access the globals just use $GLOBALS.
 [2003-08-01 15:32 UTC] cellog@php.net
This is not bogus.  Did you try the code?  The whole point is that in a function, $GLOBALS['GLOBALS'] !== $GLOBALS.  It should be the same.

In global scope, $GLOBALS['GLOBALS'] === $GLOBALS

Greg
 [2003-08-01 15:38 UTC] cellog@php.net
an interesting thing: the following code does work as expected.  Perhaps the index is not set unless $GLOBALS is accessed globally?

function not_super()
{
//    var_dump($GLOBALS['GLOBALS']);
    var_dump(isset($GLOBALS['GLOBALS']));
    $a = array_keys($GLOBALS);
    
    var_dump(isset($a['GLOBALS']));
}
not_super();
//    var_dump(isset($GLOBALS['GLOBALS']));
    $a = array_keys($GLOBALS);
    
//    var_dump(isset($a['GLOBALS']));

 [2003-08-01 15:49 UTC] iliaa@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

First of all accessing $GLOBALS as $GLOBALS['GLOBALS'] is just plain wrong, you should just use $GLOBALS. If you look @ var_dump($GLOBALS) you'll see ["GLOBALS"] is infact a reference to itself, &array().
You could do something like $GLOBALS['GLOBALS']['GLOBALS']['GLOBALS']['_ENV'] and it would work because each globals array contains a reference to itself.

This reference is only avaliable inside the global (main()) scope, which is why you cannot access it from inside the function.
 [2003-08-01 15:56 UTC] cellog@php.net
The reason this is a bug is this code deletes the $GLOBALS array, and someone might do it by accident:

<?php
function stupid()
{
    global $GLOBALS;
    var_dump($GLOBALS); // outputs NULL
}
stupid();
?>

Someone just posted to php.general asking about this exact issue, which is why I posted the bug.  Last I checked, the definition of a bug is unexpected behavior, and this qualifies.  It's rare, but perhaps there is something more critical hidden in the fact that this doesn't work?  Since you're insistent, I'll leave this as bogus, but I strongly encourage you to reconsider

Note that this code works as expected:

<?php
function worksforsomereason()
{
    global $GLOBALS;
    var_dump($GLOBALS);
}
worksforsomereason();
$GLOBALS;
?>

Once $GLOBALS has been overwritten, it is irretrievable within function scope.  At the very least, a warning or notice should be thrown.  This is a bug.  If it is not to be fixed, the documentation might use a warning for dummies.

Greg
 [2003-08-01 20:26 UTC] sniper@php.net
cellog@php.net: every example you have there are not stupid, they're EXTREMELY stupid. why would you make $GLOBALS global?! Anybody doing that alone should be spanked at least.. :)

That said, there is NO bug here, move along..


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 14:01:29 2024 UTC