note 19851 added to language.references.return
| From: | gabebSPAM dot BITES dot ASS at canada dot com dot hate dot spam | Date: | Wed, 13 Mar 2002 07:34:36 +0000 |
| Subject: | note 19851 added to language.references.return | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-27708@lists.php.net to get a copy of this message | ||
References are screwy when returned from object methods. Witness the following (a hypothetical class
to illustrate the trouble):
---------
class StringList
{
var $strings = array();
function &AddString( $a_string )
{
array_push( $this->strings, $a_string);
return( $this->strings[count($this->strings)-1] );
}
}
$stringvar = "Hello World" ;
$x = new StringList;
$y = &$x->AddString( $stringvar );
$y = "I love PHP!" ;
/* This should NOT fail, but it does! */
assert( $y == $x->strings[0] );
---------
One could reasonably infer that because $y is a reference to $x->strings[0], modifying $y would
also cause $x->strings[0] to be modified.
THIS IS NOT THE CASE. After the call to AddString, $y is a COPY of $x->strings[0], which is NOT
modified when we change $y's value.
Definitely counter-intuitive and odd. This is in PHP 4.1.2.
--
http://www.php.net/manual/en/language.references.return.php
http://master.php.net/manage/user-notes.php?action=edit+19851
http://master.php.net/manage/user-notes.php?action=delete+19851
http://master.php.net/manage/user-notes.php?action=reject+19851