1

This is related to question: How does the "&" operator work in a PHP function?

Is there simple code to show the difference between

passing the object as reference

vs

passing the object's reference as value?

Community
  • 1
  • 1
nonopolarity
  • 130,775
  • 117
  • 415
  • 675
  • Hasn't Konrad Rudolph already provided [an example](http://pastie.org/1229473)? – BoltClock Oct 18 '10 at 09:26
  • interesting... I thought in the C community, passing an object by reference is the same as passing its reference as value... what I see in the above code example would be called "passing its reference as reference" – nonopolarity Oct 18 '10 at 09:30
  • You can see this link for example : http://stackoverflow.com/questions/879/are-php-variables-passed-by-value-or-by-reference – Mahsin Jul 25 '16 at 12:34

3 Answers3

2

You can pass a variable to a function by reference. This function will be able to modify the original variable.

You can define the passage by reference in the function definition:

<?php
function changeValue(&$var)
{
    $var++;
}

$result=5;
changeValue($result);

echo $result; // $result is 6 here
?>
Mahsin
  • 618
  • 4
  • 14
0
<?php
class X {
    var $abc = 10; 
}
class Y {
    var $abc = 20; 
    function changeValue(&$obj){//1>here the object,$x is a reference to the object,$obj.hence it is "passing the object's reference as value"
        echo 'inside function :'.$obj->abc.'<br />';//2>it prints 10,bcz it accesses the $abc property of class X, since $x is a reference to $obj.
        $obj = new Y();//but here a new instance of class Y is created.hence $obj became the object of class Y.
        echo 'inside function :'.$obj->abc.'<br />';//3>hence here it accesses the $abc property of class Y.
    }
}
$x = new X();
$y = new Y();

$y->changeValue($x);//here the object,$x is passed as value.hence it is "passing the object as value"
echo $x->abc; //4>As the value has been changed through it's reference ,hence it calls $abc property of class Y not class X.though $x is the object of class X
?>

o/p :

inside function :10
inside function :20
20
Ipsita Rout
  • 4,149
  • 3
  • 33
  • 37
0

How about this:

<?php
class MyClass {
    public $value = 'original object and value';
}

function changeByValue($originalObject) {
    $newObject = new MyClass();
    $newObject->value = 'new object';

    $originalObject->value = 'changed value';

    // This line has no affect outside the function, and is
    // therefore redundant here (and so are the 2 lines at the
    // the top of this function), because the object
    // "reference" was passed "by value".
    $originalObject = $newObject;
}

function changeByReference(&$originalObject) {
    $newObject = new MyClass();
    $newObject->value = 'new object';

    $originalObject->value = 'changed value';

    // This line changes the object "reference" that was passed
    // in, because the "reference" was passed "by reference".
    // The passed in object is replaced by a new one, making the
    // previous line redundant here.
    $originalObject = $newObject;
}

$object = new MyClass();
echo $object->value;  // 'original object and value';

changeByValue($object)
echo $object->value;  // 'changed value';

$object = new MyClass();
echo $object->value;  // 'original object and value';

changeByReference($object)
echo $object->value;  // 'new object';
Dan King
  • 2,915
  • 3
  • 19
  • 21