note 78799 deleted from language.references.pass by googleguy
| From: | googleguy@php.net | Date: | Sun, 23 Feb 2014 17:15:18 +0000 |
| Subject: | note 78799 deleted from language.references.pass by googleguy | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-198603@lists.php.net to get a copy of this message | ||
Note Submitter: Chuckie
----
If you intend to pass a copy of an object to a function, then you should use 'clone' to
create a copy explicity. In PHP5, objects appear to always be passed by reference (unlike PHP4),
but this is not strictly true.
The way I think of it is that if you use '=&' (or you explicitly pass to a function by
reference) the variable behaves like a C++ reference, except that you can re-assign the reference to
something else (which is not possible in C++).
When you use '=' with an object, it is more like you are dealing with a pointer (if you
think in this way, the '->' access element through pointer operator has the same
behaviour in C++).
<?php
class z {
public $var = '';
}
function f(&$obj1, $obj2, $obj3, $obj4) {
$obj1->var = null;
$obj2->var = null;
$obj3 = new z();
$obj3->var = null;
$obj4 = clone $obj4;
$obj4->var = null;
}
$a = new z();
$b = new z();
$c = new z();
$d = new z();
f($a, $b, $c, $d);
var_dump($a); // object(z)#1 (1) { ["var"] => NULL }
var_dump($b); // object(z)#2 (1) { ["var"] => NULL }
var_dump($c); // object(z)#3 (1) { ["var"] => string(0) "" }
var_dump($d); // object(z)#4 (1) { ["var"] => string(0) "" }
?>
Stephen
08-Jul-2007 04:54
jcastromail at yahoo dot es stated:
****
in php 5.2.0 for classes
$obj1 = $obj2;
is equal to
$obj1 = &$obj2;"
****
However, that is not completely true. While both = and =& will make a variable refer to the same
object as the variable being assigned to it, the explicit reference assignment (=&) will keep
the two variables joined to each other, whereas the assignment reference (=) will make the assigned
variable an independent pointer to the object. An example should make this clearer:
<?php
class z {
public $var = '';
}
$a = new z();
$b =& $a;
$c = $a;
$a->var = null;
var_dump($a);
print '<br>';
var_dump($b);
print '<br>';
var_dump($c);
print '<br><br>';
$a = 2;
var_dump($a);
print '<br>';
var_dump($b);
print '<br>';
var_dump($c);
print '<br><br>';
?>
This outputs:
object(z)#1 (1) { ["var"]=> NULL }
object(z)#1 (1) { ["var"]=> NULL }
object(z)#1 (1) { ["var"]=> NULL }
int(2)
int(2)
object(z)#1 (1) { ["var"]=> NULL }
So although all 3 variables reflect changes in the object, if you reassign one of the variables that
were previously joined by reference to a different value, BOTH of those variables will adopt the new
value.
Perhaps this is because =& statements join the 2 variable names in the symbol table, whereas =
statements applied to objects simply create a new independent entry in the symbol table that simply
points to the same location as other entries. I don't know for sure - I don't think this
behavior is documented in the PHP manual, so perhaps somebody with more knowledge of PHP's
internals can clarify what is going on.