62

is there a way to create my own custom superglobal variables like $_POST and $_GET?

Moon
  • 22,195
  • 68
  • 188
  • 269

11 Answers11

52

Static class variables can be referenced globally, e.g.:

class myGlobals {

   static $myVariable;

}

function a() {

  print myGlobals::$myVariable;

}
Scott Reynen
  • 3,530
  • 18
  • 17
  • 2
    You sir, made my day. But does it only work inside classes? Is there a way to do this outside a class? – Mauker Jun 18 '15 at 14:36
37

Yes, it is possible, but not with the so-called "core" PHP functionalities. You have to install an extension called runkit7: Installation

After that, you can set your custom superglobals in php.ini as documented here: ini.runkit.superglobal

Umair Khan
  • 1,684
  • 18
  • 34
Piero Mori
  • 449
  • 4
  • 4
  • 7
    This is the only answer on this question that actually answers it. The other ones are basically 'no, but you can do this instead'. +1, I wish it were higher. – Chiri Vulpes Mar 15 '15 at 11:13
  • For Swoole's HTTP Server, if we need to set Super Globals, runkit is also best use case there. – Fakhar Anwar Jul 24 '20 at 09:04
19

I think you already have it - every variable you create in global space can be accessed using the $GLOBALS superglobal like this:

// in global space
$myVar = "hello";

// inside a function
function foo() {
    echo $GLOBALS['myVar'];
}
OneNerd
  • 6,442
  • 17
  • 60
  • 78
  • 2
    What's the performance of using $GLOBALS array compared to simply using the variable itself? – Pacerier Jun 10 '13 at 15:46
  • 1
    ^ Same as any other nested array, it's essentially negligible. Having your own superglobal is appealing more for ease of use (less typing) and aesthetics. – simontemplar Nov 07 '13 at 01:40
  • 1
    $GLOBALS are not SUPERglobals! Every global variable must be imported into a function scope using the `global` statement. Superglobals have no such limitation. – Cláudio Silva Oct 31 '18 at 16:50
  • @Cláudio Silva $GLOBALS are super globals according to [php.net](https://www.php.net/manual/en/language.variables.superglobals.php) Accessing $GLOBALS inside a function does work. – Marcus Nov 27 '20 at 22:43
  • What I meant was the variables accessible trough $GLOBALS are those that need to be imported explicitly via `global $varName` so they are not superglobals. Also, superglobal variables are not present on $GLOBALS. But the $GLOBALS array is, indeed, superglobal. – Cláudio Silva Sep 17 '21 at 18:06
7
   Class Registry {
 private $vars = array();
 public function __set($index, $value){$this->vars[$index] = $value;}
 public function __get($index){return $this->vars[$index];}
}
$registry = new Registry;

function _REGISTRY(){
    global $registry;
    return $registry;
}

_REGISTRY()->sampleArray=array(1,2,'red','white');

//_REGISTRY()->someOtherClassName = new className;
//_REGISTRY()->someOtherClassName->dosomething();

class sampleClass {
    public function sampleMethod(){
        print_r(_REGISTRY()->sampleArray); echo '<br/>';
        _REGISTRY()->sampleVar='value';
        echo _REGISTRY()->sampleVar.'<br/>';

    }
}

$whatever = new sampleClass;

$whatever->sampleMethod();
Guntarss
  • 71
  • 1
  • 2
  • 5
    Adding a description to this answer would help people to understand what you have done. – starbeamrainbowlabs Sep 06 '16 at 10:37
  • This approach demonstrates the registry pattern and requires you to type `_REGISTRY()->` every time you want to access the variable for reading or writing. The sampleClass is a demonstration of reading and writing to the registry from within the scope of a function. Personally I'm not a fan of the registry pattern, despite resorting to it often, but I do enjoy having a dedicated class, not intended to be constructed, where I can store a bunch of static variables, as shown in the accepted answer. – Ultimater Nov 27 '18 at 05:33
7

One other way to get around this issue is to use a static class method or variable.

For example:

class myGlobals {

   public static $myVariable;

}

Then, in your functions you can simply refer to your global variable like this:

function Test()
{
 echo myGlobals::$myVariable;
}

Not as clean as some other languages, but at least you don't have to keep declaring it global all the time.

IntelliAdmin
  • 184
  • 1
  • 2
6

No

There are only built-in superglobals listed in this manual

Geo
  • 12,666
  • 4
  • 40
  • 55
5

Not really. though you can just abuse the ones that are there if you don't mind the ugliness of it.

Rik Heywood
  • 13,816
  • 9
  • 61
  • 81
2

You can also use the Environment variables of the server, and access these in PHP This is a good way to maybe store global database access if you own and exclusively use the server.

Ian Warner
  • 1,058
  • 13
  • 32
1

possible workaround with $GLOBALS:

file.php:

$GLOBALS['xyz'] = "hello";

any_included_file.php:

echo $GLOBALS['xyz'];
T.Todua
  • 53,146
  • 19
  • 236
  • 237
  • I don't think this answers the question. The point of a superglobal is that you can reference it without either `global` or `$GLOBALS`, so this is just two ways to access a normal global. – Matthijs Kooijman Apr 30 '19 at 14:38
1

One solution is to create your superglobal variable in a separate php file and then auto load that file with every php call using the auto_prepend_file directive.

something like this should work after restarting your php server (your ini file location might be different):

/usr/local/etc/php/conf.d/load-my-custom-superglobals.ini

auto_prepend_file=/var/www/html/superglobals.php

/var/www/html/superglobals.php

<?php
  $_GLOBALS['_MY_SUPER_GLOBAL'] = 'example';

/var/www/html/index.php

<?php
  echo $_MY_SUPER_GLOBAL;
DiverseAndRemote.com
  • 19,314
  • 10
  • 61
  • 70
-2

Actually, there is no direct way to define your own superglobal variables; But it's a trick that I always do to access simpler to my useful variables!

class _ {
    public static $VAR1;
    public static $VAR2;
    public static $VAR3;
}

Then I want to use:

function Test() {
    echo \_::$VAR2;
}

Notice: Don't forget to use \ before, If you want to use it everywhere you have a namespace too...

Enjoy...

MiMFa
  • 981
  • 11
  • 14