Is it possible to create a superglobal in PHP?

That is, do not create an element in one of the superglobal arrays, but create your own variable.

Answer 1, authority 100%

Well, if there are three pieces, then such a model will suit you, I think

class MyClass1 {
  static $instance = null;
  private $vars = array();
  function check() { if (self::$instance == null) self::$instance = new self; };
  function set($name, $val) {
    self::$instance->_set($name, $val);
    echo "\n".$name.' = '.$val;
    return false;
  function _set($n, $v) { $this->vars[$n] = $v; }
function myFunc() {
  MyClass::set('a', 1);
  return false;

Answer 2, authority 100%

No, PHP doesn’t have a way to create its own superglobals, nor does it need to. There are many approaches in which global variables are not needed, and in general, global variables are evil 🙂

You better describe the task for which you needed your superglobals.

Answer 3, authority 33%

Well, if you mean a variable that will retain its value even after a page reload, then sessions are just right for you. It is very easy to work with them.

Leave a reply

Please enter your comment!
Please enter your name here