note 56820 added to language.references.pass

From: Date: Thu, 15 Sep 2005 22:42:11 +0000
Subject: note 56820 added to language.references.pass
Groups: php.notes 
Request: Send a blank email to php-notes+get-95252@lists.php.net to get a copy of this message
Not sure if this is obvious to everyone, but if you pass an object by reference into a function and then set that variable to the reference of a new object, the initial variable outside the function is still pointing to the original object. It became obvious to me why this is the case when I made a little diagram of what's actually happening in the system's memory, but before that when I was just looking at the code, it took me a while of testing before I realized what was going on. Here's an example (I haven't tried in PHP5, but I would guess it works the same): <?php class Foo {} class Bar {} function & createBar() { return new Bar; } function setBar(& $obj) { echo get_class($obj) . "\n"; $obj = & createBar(); echo get_class($obj) . "\n"; } $test = new Foo; setBar($test); echo get_class($test); ?> Outputs: foo bar foo ---- Manual Page -- http://www.php.net/manual/en/language.references.pass.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+56820 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+56820&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+56820&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+56820&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+56820&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+56820&report=yes&reason=non-english Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+56820&report=yes&reason=already+in+docs Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+56820&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+56820&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#95252) next »