note 89634 deleted from language.types.object by googleguy
| From: | googleguy@php.net | Date: | Sat, 19 Jan 2013 03:45:13 +0000 |
| Subject: | note 89634 deleted from language.types.object by googleguy | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-193239@lists.php.net to get a copy of this message | ||
Note Submitter: edgar dot klerks at gmail dot com
----
I saw there was some confusing about what the & operator does in php in relation to objects. So
I made an illustrative example:
<?php
class Test {
}
class Test2 {
}
print "---\n";
/* A normal assignment makes test2 into a reference to the object
result:
---
object(Test)#1 (0) {
}
object(Test2)#2 (0) {
}
*/
$test1 = new Test;
$test2 = $test1;
$test2 = new Test2;;
print var_dump($test1);
print var_dump($test2);
/* If we use & we make test2 a reference to $test1
result:
---
object(Test2)#2 (0) {
}
object(Test2)#2 (0) {
}
try instead $test2 = 'pipo';
*/
print "---\n";
$test1 = new Test;
$test2 =& $test1;
$test2 = new Test2;
print var_dump($test1);
print var_dump($test2);
/* If we want to prevent, we ever overwrite what is in $test1, we can do the following:
$test2 =& new Test2;
I don't understand what happens here exactly.
result:
---
object(Test)#3 (0) {
}
object(Test2)#2 (0) {
}
*/
print "---\n";
$test1 = new Test;
$test2 =& $test1;
$test2 =& new Test2;
print var_dump($test1);
print var_dump($test2);
?>