note 19851 deleted from language.references.return by torben

From: Date: Wed, 13 Mar 2002 08:35:17 +0000
Subject: note 19851 deleted from language.references.return by torben
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-27709@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.

« previous php.notes (#27709) next »