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
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: rayro at gmx dot de
New email:
PHP Version: OS:

 

 [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: Sat Apr 27 18:01:35 2024 UTC