note 59820 added to language.references.spot
| From: | SergioSantana:ssantana at tlaloc dot imta dot mx | Date: | Fri, 16 Dec 2005 16:41:30 +0000 |
| Subject: | note 59820 added to language.references.spot | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-100311@lists.php.net to get a copy of this message | ||
*** WARNING about OBJECTS TRICKY REFERENCES ***
-----------------------------------------------
The use of references in the context of classes
and objects, though well defined in the documentation,
is somehow tricky, so one must be very careful when
using objects. Let's examine the following two
examples:
<?php
class y {
public $d;
}
$A = new y;
$A->d = 18;
echo "Object \$A before operation:\n";
var_dump($A);
$B = $A; // This is not an explicit (=&) reference assignment,
// however, $A and $B refer to the same instance
// though they are not equivalent names
$C =& $A; // Explicit reference assignment, $A and $C refer to
// the same instance and they have become equivalent
// names of the same instance
$B->d = 1234;
echo "\nObject \$B after operation:\n";
var_dump($B);
echo "\nObject \$A implicitly modified after operation:\n";
var_dump($A);
echo "\nObject \$C implicitly modified after operation:\n";
var_dump($C);
// Let's make $A refer to another instance
$A = new y;
$A->d = 25200;
echo "\nObject \$B after \$A modification:\n";
var_dump($B); // $B doesn't change
echo "\nObject \$A after \$A modification:\n";
var_dump($A);
echo "\nObject \$C implicitly modified after \$A modification:\n";
var_dump($C); // $C changes as $A changes
?>
Thus, note the difference between assignments $X = $Y and $X =& $Y.
When $Y is anything but an object instance, the first assignment means
that $X will hold an independent copy of $Y, and the second, means that
$X and $Y will refer to the same thing, so they are tight together until
either $X or $Y is forced to refer to another thing. However, when $Y
happens to be an object instance, the semantic of $X = $Y changes and
becomes only slightly different to that of $X =& $Y, since in both
cases $X and $Y become references to the same object. See what this
example outputs:
Object $A before operation:
object(y)#1 (1) {
["d"]=>
int(18)
}
Object $B after operation:
object(y)#1 (1) {
["d"]=>
int(1234)
}
Object $A implicitly modified after operation:
object(y)#1 (1) {
["d"]=>
int(1234)
}
Object $C implicitly modified after operation:
object(y)#1 (1) {
["d"]=>
int(1234)
}
Object $B after $A modification:
object(y)#1 (1) {
["d"]=>
int(1234)
}
Object $A after $A modification:
object(y)#2 (1) {
["d"]=>
int(25200)
}
Object $C implicitly modified after $A modification:
object(y)#2 (1) {
["d"]=>
int(25200)
}
Let's review a SECOND EXAMPLE:
<?php
class yy {
public $d;
function yy($x) {
$this->d = $x;
}
}
function modify($v)
{
$v->d = 1225;
}
$A = new yy(3);
var_dump($A);
modify($A);
var_dump($A);
?>
Although, in general, a formal argument declared
as $v in the function 'modify' shown above, implies
that the actual argument $A, passed when calling
the function, is not modified, this is not the
case when $A is an object instance. See what the
example code outputs when executed:
object(yy)#3 (1) {
["d"]=>
int(3)
}
object(yy)#3 (1) {
["d"]=>
int(1225)
}
----
Manual Page -- http://www.php.net/manual/en/language.references.spot.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+59820
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+59820&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+59820&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+59820&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+59820&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+59820&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+59820&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+59820&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+59820&report=yes
Search -- http://master.php.net/manage/user-notes.php