views:

281

answers:

6

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 ?

Example of loop:

for($i = 0 ; $i < 1000 ; $i++){
   $a = 'Fish & Chips '.$i;
   echo do_my_hash($a);
}
+11  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 thinking about performances, you should read this recent blog-post : Do not use PHP references (quoting) :

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 going after performances ;-)

Pascal MARTIN
I was thinking about efficiency. Since the function is not changing the parameter, it would be better to pass-by-reference, since it than does not need to copy of the value. If the $a="bla bla bla... [text with 10.000 chars...]"; ... 10.000 chars would take alot of memory, so instead of copying it, send it by reference...
PHP_Jedi
I would use it very sparingly, and only if you intend to give back a value similar to what you passed in. Creating an md5 hash of something is completely changing it, so you wouldn't want to pass by reference.
Chacha102
In this case, the article I linked to is something you should readlly read ;-) ;; the sentence I quoted, of course -- and there is the explanation just after *(which I didn't quote)* ;; PHP is not C, for instance ^^
Pascal MARTIN
A: 

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.

Mike Trpcic
+1  A: 

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.

middaparka
+1  A: 

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.

Cruachan
+1  A: 

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.

konforce
A: 

You might also be interested in reading this article from Sara Golemon on the topic.

Eric Butera