php PHP中全局变量和函数参数的优缺点?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2216340/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
The advantage / disadvantage between global variables and function parameters in PHP?
提问by Mohammad
sorry i'm a beginner and i can't determine how good a question this is, maybe it sounds utterly obvious to some of you.
抱歉,我是初学者,我无法确定这是一个多么好的问题,也许对你们中的一些人来说这听起来很明显。
if our useof these two below is the same which is better?
如果我们下面这两个的用法是一样的,哪个更好?
function doSomething ($var1,$var2,..){
...
}
OR
或者
function doSomething (){
global $var1,$var2,..;
...
}
by our use I mean that I know that in the second scenario we can also alter the global variables' value. but what if we don't need to do that, which is the better way of writing this function? does passing variables take less memory than announcing global's in a function?
通过我们的使用,我的意思是我知道在第二种情况下我们也可以改变全局变量的值。但是如果我们不需要这样做,那么编写这个函数的更好方法是什么?传递变量是否比在函数中声明全局变量占用更少的内存?
回答by Arkaaito
The memory usage is a paltry concern. It's much more important that the code be easy to follow and not have... unpredicted... results. Adding global variables is a VERY BAD IDEA from this standpoint, IMO.
内存使用是一个微不足道的问题。更重要的是代码易于遵循并且没有……不可预测的……结果。从 IMO 的角度来看,添加全局变量是一个非常糟糕的想法。
If you're concerned about memory usage, the thing to do is
如果你担心内存使用,要做的就是
function doSomething (&$var1, &$var2,..) {
...
}
This will pass the variables by reference and not create new copies of them in memory. If you modify them during the execution of the function, those modifications will be reflected when execution returns to the caller.
这将通过引用传递变量,而不是在内存中创建它们的新副本。如果您在函数执行期间修改它们,则这些修改将在执行返回给调用者时反映出来。
However, please note that it's very unusual for even this to be necessary for memory reasons. The usual reason to use by-reference is for the reason I listed above (modifying them for the caller). The way to go is almost always the simple
但是,请注意,由于内存原因,即使这样也是必要的,这是非常不寻常的。使用 by-reference 的通常原因是我上面列出的原因(为调用者修改它们)。要走的路几乎总是简单的
function doSomething ($var1, $var2) {
...
}
回答by Anthony Forloney
Avoid using global variables, use the passing variables in parameters approach instead. Depending on the size of your program, the performance may be negligible.
避免使用全局变量,而是使用参数方法中的传递变量。根据程序的大小,性能可能可以忽略不计。
But if you are concerned with performance here are some key things to note about global variable performancewith regards to local variables (variables defined within functions.)
但是,如果您关心性能,这里有一些关于局部变量(在函数中定义的变量)的全局变量性能需要注意的关键事项。
- Incrementing a global variable is 2 times slow than a local var.
- Just declaring a global variable without using it in a function also slows things down (by about the same amount as incrementing a local var). PHP probably does a check to see if the global exists.
- 增加全局变量比局部变量慢 2 倍。
- 仅仅声明一个全局变量而不在函数中使用它也会减慢速度(与增加局部变量大致相同)。PHP 可能会检查全局是否存在。
Also, global variables increase the risk of using wrong values, if they were altered elsewhere inside your code.
此外,全局变量会增加使用错误值的风险,如果它们在代码中的其他地方被更改。
回答by Matthew Flaschen
Write it to take parameters. Maintainability is far more important than micro-optimization. When you take parameters, the variables can not be modified in unexpected places.
编写它以获取参数。可维护性远比微优化重要。取参数时,不能在意外的地方修改变量。
回答by gssgss
Although it is not good practice as long as you guarantee that the global is never written, but only read you will have the flexibility of paramaters.
虽然这不是一个好的做法,只要你保证全局不写,但只读你会有参数的灵活性。
As as alternative, you can pass one parameter (or two if it really goes with the function, like exp) and the rest in an array of option (a bit like jquery does). This way you are not using globals, have some parameter flexibility and have clearly defined the defaults for each parameter.
作为替代方案,您可以传递一个参数(或两个,如果它确实与函数一起使用,例如 exp),其余参数则放在一个选项数组中(有点像 jquery 那样)。这样您就不用使用全局变量,具有一定的参数灵活性并明确定义了每个参数的默认值。
function get_things($thing_name,$opt= array() {
if(!isset($opt["order"])) $opt["order"]= 'ASC';
}
回答by Ilya Sheershoff
As of PHP 4 using global with big variables affects performance significantly.
从 PHP 4 开始,使用带有大变量的 global 会显着影响性能。
Having in $data a 3Mb string with binary map data and running 10k tests if the bit is 0 or 1 for different global usage gives the following time results:
在 $data 中包含一个带有二进制映射数据的 3Mb 字符串并运行 10k 测试,如果位为 0 或 1 以用于不同的全局使用,则给出以下时间结果:
function getBit($pos) {
global $data;
$posByte = floor($pos/8);
...
}
t5 bit open: 0.05495s, seek: 5.04544s, all: 5.10039s
t5 位打开:0.05495s,搜索:5.04544s,全部:5.10039s
function getBit($data) {
global $_bin_point;
$pos = $_bin_point;
$posByte = floor($pos/8);
}
t5 bit open: 0.03947s, seek: 0.12345s, all: 0.16292s
t5 位打开:0.03947s,搜索:0.12345s,全部:0.16292s
function getBit($data, $pos) {
$posByte = floor($pos/8);
...
}
t5 bit open: 0.05179s, seek: 0.08856s, all: 0.14035s
t5 位打开:0.05179s,搜索:0.08856s,全部:0.14035s
So, passing parameters is way faster than using global on variables >= 3Mb. Haven't tested with passing a $&data reference and haven't tested with PHP5.
因此,传递参数比在变量 >= 3Mb 上使用全局要快得多。尚未通过传递 $&data 引用进行测试,也未使用 PHP5 进行测试。
回答by Urda
Pass in parameters, avoid globals. Keeping only the scope you need for a given situation is a measure of good code design. You may want to look at PHP variable scope...
传入参数,避免全局变量。仅保留给定情况所需的范围是良好代码设计的衡量标准。您可能想查看 PHP 变量范围...
http://php.net/manual/en/language.variables.scope.php
http://php.net/manual/en/language.variables.scope.php
An excellent resource, with some pointers on what is best practices and memory management.
一个很好的资源,有一些关于什么是最佳实践和内存管理的指示。

