php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #67029 feature-request: user defined superglobals
Submitted: 2014-04-05 15:45 UTC Modified: 2014-04-05 21:58 UTC
From: php at richardneill dot org Assigned:
Status: Wont fix Package: Variables related
PHP Version: Irrelevant OS:
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: php at richardneill dot org
New email:
PHP Version: OS:

 

 [2014-04-05 15:45 UTC] php at richardneill dot org
Description:
------------
When writing a complex web application, it is useful to put common configuration settings, strings, database-connection resources, etc into an array, such as $APPLICATION.

However, it's then necessary to pull this in with "global $APPLICATION" into every single function. This leads to uglier code.


Test script:
---------------
global $X;                    #wish: *push* X into the global scope.
$X = 42;
function testit () {
  //global $X;                #wish: not required to pull in $X here.
  echo "X is $X";
}
function testit2 () {
  //global $X;                #... or here, or in 100 other places.
  echo "X is $X";
}
testit();
testit2);
etc


Expected result:
----------------
It would be a useful enhancement to have one of the following:

* allow variables to be "pushed" into the global scope, rather than needing to be pulled in by each function (i.e. declare the variable as global just once in the main body, rather than every single time it's needed in a function).

* have a mechanism to create user-defined superglobals 
  (it's currently documented as being explicitly not allowed).

* have PHP pre-create an empty superglobal such as $_APP[] or $_USER[] which
  is reserved for the user to do as he wishes.


Thanks for your time - I hope this suggestion is helpful.


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2014-04-05 21:58 UTC] requinix@php.net
-Status: Open +Status: Wont fix
 [2014-04-05 21:58 UTC] requinix@php.net
Global variables are bad enough without letting developers make more of them. Especially when there's no indication that a variable was defined globally. Maintenance and debugging is a nightmare when you don't know where variables come from or how they got their values.

There are plenty of better mechanisms to share data across a script: registry pattern, singletons, dependency injection, even static variables in classes...
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Nov 11 16:01:30 2024 UTC