- many forget global variables are fully global, not only to the current file;
- declaration outside any other scope in the file doesn't necessarily mean the variable will be global, e.g. if the current file is included from a non-root scope. The only completely reliable way to declare a global is to use
$GLOBALS
;
- fully global variables are generally a bad idea anyway.
Instead, if you need a similar feature, look at class (or function) static variables.
- they have the expected scope;
- support access restriction (private, protected, public);
- can be referenced from the using class with the better syntax
self::$my_class_global
.
If you don't have a class… well, first check if your code couldn't get better organized by being in one. If not, either create a state class (abstract class MyState { static $foo = 0; }
) and use it as a global, or know what you do and use a global, with all its attached baggage.