php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #64153 Creating own super globals
Submitted: 2013-02-05 11:08 UTC Modified: 2013-02-05 14:14 UTC
From: rayro at gmx dot de Assigned:
Status: Wont fix Package: Class/Object related
PHP Version: Irrelevant OS: Irrelevant
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please — but make sure to vote on the bug!
Your email address:
MUST BE VALID
Solve the problem:
15 + 3 = ?
Subscribe to this entry?

 
 [2013-02-05 11:08 UTC] rayro at gmx dot de
Description:
------------
It would be a nice feature to create super globals at runtime like $_SERVER or $_SESSION without making the use of extensions like "runkit". I think it was runkit that enables this feature, or am i wrong?

Generally the "static" keyword is a good one to make this possible. It has currently no effect when using in global scope (no error, no warning, no notice).

What about to use static for defining super globals? I think it is self-explaining to the people out there...

Test script:
---------------
<?php
static $_SUPER = array('foo' => 'bar');
class A
{
  function __construct()
  {
    echo($_SUPER['foo']);
  }
}
$obj = new A;
?>

Expected result:
----------------
string(3) "bar"

Actual result:
--------------
Notice: Undefined variable: _SUPER in ... on line ...

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-02-05 14:14 UTC] nikic@php.net
-Status: Open +Status: Wont fix
 [2013-02-05 14:14 UTC] nikic@php.net
We definitely don't want to make using globals *easier*.

Apart from that, if you really want something like userland superglobals (for reasons that are elusive to me), you can simply use a shorter function alias and get more or less the same effect:

    function &_super() {
        return $GLOBALS['_SUPER'];
    }

    // Usage:
    _super()['foo'] = 'bar';
    echo _super()['foo'];

Or with a small shortcut for access:

    function _super($offset = null) {
        if ($offset === null) {
            return $GLOBALS['_SUPER'];
        } else {
            return $GLOBALS['_SUPER'][$offset];
        }
    }

    // This way you can also write
    $foo = _super('foo');
    // instead of
    $foo = _super()['foo'];
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 25 05:01:33 2024 UTC