Question

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!

Était-ce utile?

La solution

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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top