3

Recently i was studying the "Passing by Reference", I come to know following ways

What is the main difference between the following methods.

1.

function foo(&$var)
{
    $var++;
}

$a=5;
foo($a);

2.

function foo($var)
{
    $var++;
}

$a=5;
foo(&$a);

3.

function foo(&$var)
{
    $var++;
}
function &bar()
{
    $a = 5;
    return $a;
}
foo(bar());

even though all of them produce same results, and which is the best way to work with.

Thanks.

Chetan Sharma
  • 2,539
  • 5
  • 25
  • 41

3 Answers3

5
function foo(&$var)
{
    $var++;
}

$a=5;
foo($a);

This accepts a parameter that is always passed by reference (the & is in foo(&$var)). When $a is passed, it's always as a reference, so incrementing the variable inside the function will cause the parameter to be modified.


function foo($var)
{
    $var++;
}

$a=5;
foo(&$a);

Do not use this. This is call-time pass-by-reference (you're passing &$a, a reference to $a, into the function), and is deprecated as of PHP 5.3.0. It's bad practice because the function doesn't expect a reference.


function foo(&$var)
{
    $var++;
}
function &bar()
{
    $a = 5;
    return $a;
}
foo(bar());

This returns a reference (the & is in &bar()) to a variable $a declared in the function bar(). It then takes a reference to the return value of bar() and increments it. I'm not sure at a glance why this would be useful, though, especially for primitive/scalar types.

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
  • 2
    The only case where I ever had to use return by ref is with magic getters: `&__get`. Otherwise `$obj->inaccessibleProperty[]` will not work for example ;) – NikiC Oct 03 '10 at 18:41
3

The second method is deprecated and should never be used.

Typically the function should just return the value.

function foo($a)
{
   $a = 5;
}

$a = foo($a);

That's basically what the third method is doing. Not sure why you included an embedded pass by reference.

Pass by reference (for scalars and arrays) should generally be avoided because it's less clear than returning the value. However, it can be useful in cases where you need to modify multiple values within one function call.

Note that in PHP5, there's not even a need to explicitly pass an object by reference if you simply want to modify the original object, as the handle to the object will point to the same object as was passed to the function.

Matthew
  • 47,584
  • 11
  • 86
  • 98
  • 1
    *sigh* Objects are not passed by reference in PHP 5. References to the objects are passed by value. There are substantial differences. – Artefacto Oct 03 '10 at 19:00
  • I've corrected my misleading statement. The only point I was trying to make was that you don't need to pass by reference to avoid an object copy in PHP5. There is, of course, still a difference when using a reference to an object in PHP5. – Matthew Oct 03 '10 at 19:21
1

The last example is not equivalent to the first two. If you print the value of $a after calling foo, you will see that it is not defined. The third is basically an obfuscated no-op.

Kip
  • 107,154
  • 87
  • 232
  • 265