PHP set global variable as handle for class
Frage
For some reason, when defining:
function __construct() {
if(!isset($_GLOBALS["className"])) {
$_GLOBALS["className"] = new className;
}
return true;
}
$_GLOBALS["className"]->classMethod();
PHP for some reason states that $_GLOBALS["className"] is undefined.
Oh, and the same also occurs even if I set the global value to something else, from within that class. I can test the value of the global through the construct or some other method, but not outside - it seems that the global is lost outside the class for some reason.
Is there a way to retain the global after declaring it from within an external class? Any help is sincerely appreciated!
Lösung
You want to use $GLOBALS
and not $_GLOBALS
:
http://php.net/manual/en/reserved.variables.globals.php
$_GLOBALS
will just be available in your function scope.
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow