note 99029 added to language.references.arent

From: Date: Thu, 22 Jul 2010 23:02:42 +0000
Subject: note 99029 added to language.references.arent
Groups: php.notes 
Request: Send a blank email to php-notes+get-171063@lists.php.net to get a copy of this message
@russrobinson Your example does not seem to show any difference between objects and non objects. It simply shows that passing by reference to a function works as it should, such that assignment to a reference will change the value of the referenced variable. Here, the same behavior is exhibited by non-object variables passed by reference to a function: <?php function renameByReference(&$name) { // assign a new value to the reference $name = 'Spike'; } $dogName = "Garfield"; renameByReference($dogName); print "My dog's name is $dogName"; // outputs: My dog's name is Spike ?> What the documentation in the manual states is that... "There's no way to bind $bar in the calling scope to something else using the reference mechanism..." That is.. to assign a NEW reference to an OLD reference does not bind the old referenced variable with the new referenced variable. <?php function tryRenameByReference(&$name) { // at this point, $name references $dogName in the calling scope $genericDogName = 'Spike'; // $name gets reassigned a NEW reference to $genericDogName $name =& $genericDogName; // at this point, $name NO LONGER references $dogName in the calling scope, it now only references $genericDogName print "Inside, the name is $name\n"; } $dogName = "Garfield"; tryRenameByReference($dogName); print "Outside, the name is still $dogName"; /* Outputs: Inside, the name is Spike Outside, the name is still Garfield */ ?> ---- Server IP: 69.147.83.197 Probable Submitter: 206.173.192.233 ---- Manual Page -- http://www.php.net/manual/en/language.references.arent.php Edit -- https://master.php.net/note/edit/99029 Del: integrated -- https://master.php.net/note/delete/99029/integrated Del: useless -- https://master.php.net/note/delete/99029/useless Del: bad code -- https://master.php.net/note/delete/99029/bad+code Del: spam -- https://master.php.net/note/delete/99029/spam Del: non-english -- https://master.php.net/note/delete/99029/non-english Del: in docs -- https://master.php.net/note/delete/99029/in+docs Del: other reasons-- https://master.php.net/note/delete/99029 Reject -- https://master.php.net/note/reject/99029 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#171063) next »