Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When is it good to use pass by reference in PHP?

Tags:

php

People also ask

Should you pass by reference in PHP?

The short answer is use references when you need the functionality that they provide. Don't think of them in terms of memory usage or speed. Pass by reference is always going to be slower if the variable is read only. Everything is passed by value, including objects.

When should passing by reference be used?

Use pass-by-reference if you want to modify the argument value in the calling function. Otherwise, use pass-by-value to pass arguments. The difference between pass-by-reference and pass-by-pointer is that pointers can be NULL or reassigned whereas references cannot.

Is passing by reference faster PHP?

The bigger the array (or the greater the count of calls) the bigger the difference. So in this case, calling by reference is faster because the value is changed inside the function.

What is the advantage of passing by reference?

Here are some advantages of passing by reference: No new copy of variable is made, so overhead of copying is saved. This Makes program execute faster specially when passing object of large structs or classes. Array or Object can be pass.


The following does not apply to objects, as it has been already stated here. Passing arrays and scalar values by reference will only save you memory if you plan on modifying the passed value, because PHP uses a copy-on-change (aka copy-on-write) policy. For example:

# $array will not be copied, because it is not modified.
function foo($array) {
    echo $array[0];
}

# $array will be copied, because it is modified.
function bar($array) {
    $array[0] += 1;
    echo $array[0] + $array[1];
}

# This is how bar shoudl've been implemented in the first place.
function baz($array) {
    $temp = $array[0] + 1;
    echo $temp + $array[1];
}


# This would also work (passing the array by reference), but has a serious 
#side-effect which you may not want, but $array is not copied here.
function foobar(&$array) {
    $array[0] += 1;
    echo $array[0] + $array[1];
}

To summarize:

  • 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.

  • 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.

  • IMHO 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.


The short answer is use references when you need the functionality that they provide. Don't think of them in terms of memory usage or speed. Pass by reference is always going to be slower if the variable is read only.

Everything is passed by value, including objects. However, it's the handle of the object that is passed, so people often mistakenly call it by-reference because it looks like that.

Then what functionality does it provide? It gives you the ability to modify the variable in the calling scope:

class Bar {}
$bar = new Bar();

function by_val($o) { $o = null; }
function by_ref(&$o) { $o = null; }

by_val($bar); // $bar is still non null
by_ref($bar); // $bar is now null

So if you need such functionality (most often you do not), then use a reference. Otherwise, just pass by value.

Functions that look like this:

$foo = modify_me($foo);

sometimes are good candidates for pass-by-reference, but it should be absolutely clear that the function modifies the passed in variable. (And if such a function is useful, often it's because it really ought to just be part of some class modifying its own private data.)


In PHP :

  • objects are passed by reference1 -- always
  • arrays and scalars are passed by value by default ; and can be passed by reference, using an & in the function's declaration.


For the performance part of your question, PHP doesn't deal with that the same way as C/C++ ; you should read the following article : Do not use PHP references


1. Or that's what we usually say -- even if it's not "completely true" -- see Objects and references