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 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:
38 - 24 = ?
Subscribe to this entry?

 
 [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

Add a Patch

Pull Requests

Add a Pull Request

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: Tue Apr 16 21:01:28 2024 UTC