开发者

When to pass-by-reference in PHP

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(开发者_JAVA技巧$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 ?

Example of loop:

for($i = 0 ; $i < 1000 ; $i++){
   $a = 'Fish & Chips '.$i;
   echo do_my_hash($a);
}


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_hash function doesn't modify the value you're passing to it; so, I wouldn't use a reference.

And if you're concerned about performance, you should read this recent blog post: Do not use PHP references:

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 ;-)


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:

Do not use 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.


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜