I've used both methods quite a bit but I recently wondered why I hadn't standardized on one or the other, or if there was a best practice and what the reasons behind it are.
While the returning references page at PHP states quite clearly, with respect to returning references:
Only return references when you have a valid technical reason to do so.
...the passing by reference page isn't as clear when talking about parameters and return methods.
Consider the following two examples:
Referenced return
class foo {
public static function bar(&$val) {
$val++;
}
}
$input = 0;
foo::bar($input);
echo $input; // outputs 1
Explicit return
class foo {
public static function bar($val) {
$val++;
return $val;
}
}
$input = 0;
$input = foo::bar($input);
echo $input; // outputs 1
Both methods perform the same task and return the same value, but they accomplish it in only slightly different ways. I know that this scenario (multiple solutions to same task) is common in PHP, but my question is...
Is one of the above two methods preferred over the other and if so why?
I should point out that I did see other questions like this one: What's the difference between passing by reference vs. passing by value? but they deal with effects and examples, not why one is preferred over the other.
EDIT: To save any others the hassle of reading through the long discussion regarding the merits of the question...
I was hoping for something specific when taking into consideration the overhead or optimizations inherent in the underlying code whether that be PHP or C, and how that effect would translate to performance after being converted to OpCode and put in the context of potentially thousands of app servers. Small optimizations can have a big effect on the bottom line at the end of the month.
I didn't expect the reluctance, but my guess is that at first glance there isn't a difference when you're considering basic programming requirements. Ours is a different circumstance and so I should have been even more specific up front.
If anyone else cares to weigh in, I'm all ears.