如何定义全局变量?
那么您更喜欢如何声明和使用全局变量呢? 1)
global variable;
echo $variable;
或 2)
echo $GLOBALS['variable'];
?
哪种方法不太糟糕? :)
编辑:
或3)
class myglobalstuff{
static $instance;
public static function foo(){
if(!(self::$instance instanceof self))
self::$instance = new self();
return self::$instance;
}
}
...
?
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;
}
}
...
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
使用
$GLOBALS
没那么糟糕。但如果可以的话,请避免全局变量:)Using
$GLOBALS
is less bad. But if you can, avoid globals :)PHP手册解释了如何定义和使用全局变量: $GLOBAL
The PHP manual explains how to define and use global variables: $GLOBAL
global
关键字本身不会创建全局变量,它只是告诉脚本在全局范围内处理该变量。$GLOBALS
数组实际上是一个超全局变量,略有不同。更多信息在这里,这是一个很好的阅读: http://php.net/manual/ en/language.variables.scope.php
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
我们宁愿根本不使用全局变量。对于单例和注册表以及拥有全局状态的所有其他方式也是如此。
如果您学会了如何编写正确的 OOP 代码,您的情况会好得多。
更新
几个链接和相关信息:
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:
先把全局变量有多么邪恶放在一边,让我将其分解为我发现的一个更普遍的问题:使用字符串数组键而不是变量。
输入字符串数组键可能会导致难以发现的拼写错误:
$GLOBALS['var1']
vs$GLOBALS['varl']
(one vs lamda)。如果您有一个支持自动完成功能的现代 IDE,您会发现声明
global $variable
非常有用,然后在输入时调用自动完成功能以获取您没有输入错误的指示。这样的现代 IDE 还可能会出现突出显示,这将有助于避免拼写错误(您可以查看它是否出现在附近)以及导航代码。同样,它仅适用于变量,不适用于数组键。
如果您需要使用全局变量,那么使用
$GLOBALS
可能会更好地提醒您在寻找某些值时变量来自哪里。然而,至少将变量名称定义为常量,以利用 IDE 自动完成和事件突出显示可能会有所回报:$GLOBALS[_VAR1]
。当然,这些漂亮的功能也适用于类变量,这是考虑重构的另一个原因:)
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 :)
我会选择
$_SESSION['var']
而不是$GLOBALS
。它的作用或多或少相同,但更安全。编辑:我的意思是说更优选的方法。但据说 $GLOBALS 很快就会贬值。
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.
另外,忽略“无全局变量”的论点,我想对“全局”声明的使用进行精细区分,因为我已经看到它被滥用了。至少在 PHP 5 中,在全局作用域代码(不在函数或方法声明内的代码)中执行以下操作是没有意义的:
$foo 是全局的,因为它是全局声明的(不是因为“全局”关键字)。人们可以在需要引用全局 $foo 的函数或方法中使用“全局”(否则将存在本地 $foo)。
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):
$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).