note 99029 deleted from language.references.arent by danbrown
| From: | danbrown@php.net | Date: | Thu, 22 Jul 2010 23:20:10 +0000 |
| Subject: | note 99029 deleted from language.references.arent by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-171064@lists.php.net to get a copy of this message | ||
Note Submitter: AlexPHP
----
@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
*/
?>