create superglobal variables in php?

   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();

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'];
}

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

class myGlobals {

   static $myVariable;

}

function a() {

  print myGlobals::$myVariable;

}

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

Tags:

Php