php when pass by reference

1. Objects should be passed by reference always, arrays and scalars are passed by value.
2. If you are working on a very large array and plan on modifying it inside a function, you actually should use a reference to prevent it from getting copied, which can seriously decrease performance or even exhaust your memory limit.
3. If it is avoidable though (that is small arrays or scalar values), I’d always use functional-style approach with no side-effects, because as soon as you pass something by reference, you can never be sure what passed variable may hold after the function call, which sometimes can lead to nasty and hard-to-find bugs.
4. Scalar values should never be passed by reference, because the performance impact can not be that big as to justify the loss of transparency in your code.

Leave a Reply