note 89634 added to language.types.object

From: Date: Tue, 17 Mar 2009 15:04:24 +0000
Subject: note 89634 added to language.types.object
Groups: php.notes 
Request: Send a blank email to php-notes+get-152046@lists.php.net to get a copy of this message
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); ?> ---- Server IP: 83.137.20.135 Probable Submitter: 92.65.202.85 ---- Manual Page -- http://www.php.net/manual/en/language.types.object.php Edit -- https://master.php.net/note/edit/89634 Del: integrated -- https://master.php.net/note/delete/89634/integrated Del: useless -- https://master.php.net/note/delete/89634/useless Del: bad code -- https://master.php.net/note/delete/89634/bad+code Del: spam -- https://master.php.net/note/delete/89634/spam Del: non-english -- https://master.php.net/note/delete/89634/non-english Del: in docs -- https://master.php.net/note/delete/89634/in+docs Del: other reasons-- https://master.php.net/note/delete/89634 Reject -- https://master.php.net/note/reject/89634 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#152046) next »