The way you’ve done it above should work. Once a variable is declared in global scope, it should be globally available (of course, your functions still need to declare it as a global: global $x;
). You could also use the $GLOBALS
superglobal variable:
global $x;
$x->var;
is the same as
$GLOBALS['x']->var;
I personally would use a static variable in your function:
function my_data(){
static $data = null;
if(null === $data)
$data = new MY_Class();
return $data;
}
Then anywhere in your code, you could just write $vars = my_data();
and have the one instantiation of your class.
Hope that helped!