何时在 PHP 中传递引用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2157799/
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
When to pass-by-reference in PHP
提问by Vidar Vestnes
Im wondering if its good practice to pass-by-reference when you are only reading a variable, or if it should always be passed as a value.
我想知道当你只读取一个变量时按引用传递是否是好的做法,或者它是否应该始终作为值传递。
Example with pass-by-reference:
通过引用传递的示例:
$a = 'fish and chips';
$b = do_my_hash($a);
echo $b;
function &do_my_hash(&$value){
return md5($value);
}
Example with pass-by-value:
带值传递的示例:
$a = 'fish and chips';
$b = do_my_hash($a);
echo $b;
function do_my_hash($value){
return md5($value);
}
Which is better ? E.g if I was to run a loop with 1000 rounds ?
哪个更好 ?例如,如果我要运行 1000 轮循环?
Example of loop:
循环示例:
for($i = 0 ; $i < 1000 ; $i++){
$a = 'Fish & Chips '.$i;
echo do_my_hash($a);
}
回答by Pascal MARTIN
If you mean to pass a value(so the function doesn't modify it), there is no reason to pass it by reference : it will only make your code harder to understand, as people will think "this function could modify what I will pass to it — oh, it doesn't modify it?"
如果你想传递一个值(所以函数不会修改它),没有理由通过引用传递它:它只会让你的代码更难理解,因为人们会认为“这个函数可以修改我想要的传给它——哦,它不修改它?”
In the example you provided, your do_my_hashfunction doesn't modify the valueyou're passing to it; so, I wouldn't use a reference.
在您提供的示例中,您的do_my_hash函数不会修改您传递给它的值;所以,我不会使用参考。
And if you're concerned about performance, you should read this recent blog post: Do not use PHP references:
如果您担心性能,您应该阅读最近的这篇博文:不要使用 PHP 引用:
Another reason people use reference is since they think it makes the code faster. But this is wrong. It is even worse: References mostly make the code slower! Yes, references often make the code slower - Sorry, I just had to repeat this to make it clear.
人们使用引用的另一个原因是因为他们认为它可以使代码更快。但这是错误的。更糟糕的是:引用通常会使代码变慢!是的,引用通常会使代码变慢 - 抱歉,我只需要重复一遍以使其清楚。
Actually, this article might be an interesting read, even if you're not primarily concerned about performance ;-)
实际上,即使您不是主要关心性能,这篇文章也可能是一篇有趣的读物 ;-)
回答by Matthew
PHP makes use of copy-on-write as much as possible (whenever it would typically increase performance) so using references is not going to give you any performance benefit; it will only hurt. Use references only when you really need them. From the PHP Manual:
PHP 尽可能多地使用写时复制(只要它通常会提高性能),因此使用引用不会给您带来任何性能优势;它只会伤害。仅在真正需要时才使用引用。来自 PHP 手册:
Do notuse return-by-reference to increase performance. The engine will automatically optimize this on its own. Only return references when you have a valid technical reason to do so.
千万不能使用返回的引用,以提高性能。引擎将自动对此进行优化。仅在您有有效的技术原因时才返回引用。
回答by Cruachan
Good programming practice is always to pass by value whenever you can, and if you have to modify a single value it's generally better to return the modified value as a result of a function rather than pass the value by reference.
良好的编程习惯总是尽可能按值传递,如果必须修改单个值,通常最好将修改后的值作为函数的结果返回,而不是按引用传递值。
The only cases where you may need to pass by reference is where you need to modify multiple values. However these cases tend to be rare and usually should be treated as a flag to check you code because there's probably a better way of approaching the problem.
您可能需要通过引用传递的唯一情况是您需要修改多个值。然而,这些情况往往很少见,通常应该被视为检查代码的标志,因为可能有更好的方法来解决问题。
Back in the day early programming languages always used to pass by reference and passing by value was a later development to tackle the problems that this produced (you tend to end up with obscure bugs because sooner or later some programmer puts in code to modify the passed by reference value in some function or other and then it's tricky to identify where and fix properly - you tend to end up with multiple, obscure, dependencies). Consequently it's pretty perverse really to seriously consider this as an option for shaving a few machine cycles when we're multiple generations of processor beyond the point when it was considered to be a good trade-off of cpu vs complexity to aid clean, maintainable, code.
回到过去,早期的编程语言总是习惯于通过引用传递和通过值传递是后来的开发,以解决由此产生的问题(您往往最终会遇到模糊的错误,因为迟早有些程序员会放入代码来修改传递的通过某些函数或其他函数中的引用值,然后很难确定在哪里并正确修复 - 你往往最终会得到多个、模糊的依赖项)。因此,当我们是多代处理器时,认真考虑将其作为减少几个机器周期的选项是非常不正常的,因为它被认为是 cpu 与复杂性的良好权衡以帮助清洁、可维护,代码。
回答by John Parker
The joy of micro-optimisation. :-)
微优化的乐趣。:-)
To be honest, there's probably not a great deal to be gained by passing 'normal' variables by reference (unless you want to affect their value in their original scope). Also, since PHP 5 objects are automatically passed by reference.
老实说,通过引用传递“正常”变量可能不会有太多好处(除非您想影响它们在原始范围内的值)。此外,由于 PHP 5 对象是自动通过引用传递的。
回答by Mike Trpcic
Passing by reference offers no benefit if you don't want to modify that value inside the function. I try to use pass-by-value as much as possible, as it's much easier to read, and the flow of the script is more consistent.
如果您不想在函数内修改该值,则通过引用传递没有任何好处。我尽量使用传值,因为它更容易阅读,并且脚本的流程更一致。

