How do you define global variables?
so how do you prefer to declare and use global variables? 1)
global variable;
echo $variable;
or 2)
开发者_高级运维echo $GLOBALS['variable'];
?
Which is the less bad method? :)
edit:
or 3)
class myglobalstuff{
static $instance;
public static function foo(){
if(!(self::$instance instanceof self))
self::$instance = new self();
return self::$instance;
}
}
...
?
Using $GLOBALS
is less bad. But if you can, avoid globals :)
The PHP manual explains how to define and use global variables: $GLOBAL
The global
keyword doesn't create a global variable as such, it simply tells the script to treat the variable with global scope.
The $GLOBALS
array is actually a superglobal variable, which is slightly different.
More info here, it's a good read: http://php.net/manual/en/language.variables.scope.php
We prefer to not use global variables at all. And same goes for Singletons and Registries and every other way of having a global state.
You would be much better off if you learned how to write a proper OOP code.
update
Few links and related information:
- Untestable Code
- Global Variables Ar Bad
- The Clean Code Talks - "Global State and Singletons" (video)
- TotT: Using Dependancy Injection to Avoid Singletons
- Avoid Singletons
- Why Singletons are Evil
- Singleton vs. Just Create One
Putting aside for a minute how evil global variables may be, let me break this down to a more generic problem that I've spotted: using string array keys instead of variables.
Typing string array keys may lead to typos which may be tricky to spot: $GLOBALS['var1']
vs $GLOBALS['varl']
(one vs lamda).
If you have a modern IDE that does autocompletion you will find it useful to declare global $variable
and then, when typing it in, to invoke autocompletion to get an indication you didn't make a typo.
Such a modern IDE may also have occurences highlighting which will help with avoiding typos (you can see if it appears nearby) as well as navigating your code. It, again, will only work with variables and not array keys.
If you need to use globals then using $GLOBALS
may be better in reminding you where the variable came from when you're hunting down some value. It might, however, pay off to atleast define the variable names as a constant to take advantage of IDE autocomplete and occurence highlighting: $GLOBALS[_VAR1]
.
Ofcourse these nifty features also work with class variables which is yet another reason to consider refactoring :)
I'd go with $_SESSION['var']
over $GLOBALS
. It does more or less the same, but safer.
Edit: I meant to say more preferred method. But $GLOBALS was said to be depreciated soon anyway.
Also, ignoring the "no globals" argument, I'd like to make a fine distinction about the use of the "global" declaration, because I've seen it misused all over. At least in PHP 5, it is pointless to do the following in globally scoped code (code not inside a function or method declaration):
global $foo;
$foo = "someValueForEverything";
$foo is global because it is declared globally (not because of the "global" keyword). One would use "global" inside a function or method that needs to reference the global $foo (else a local $foo would come into existence).
精彩评论