I recently found this while going through PHP’s online documentation:
Do not use return-by-reference to increase performance, the engine is smart enough to optimize this on its own. Only return references when you have a valid technical reason to do it!
So, the question becomes, what is a valid technical reason to use references? Objects are already passed and returned by reference, so using references for them is a waste. In the last couple years of PHP coding, I have only found one good use for references:
1 2 3 4 5 6 7 | public function getNextLine(&$str){ .... $next_line = substr($str,0,10); $str = substr($str, 10); .... return $next_line; } |
Parsing functions are the only place that I have effectively used references. Even then, there are perfectly viable alternative solutions, like returning an array of the next line and string.
So, the moral of the story is: try to stay away from references in most cases. They can confuse the code, without adding much benefit.


Friday, February 23rd, 2007, 3:20 pm | 



March 28, 2007 at 7:39 pm