Bug #16033: Can't return reference to class member variable from class method.

From: Date: Wed, 13 Mar 2002 07:45:25 +0000
Subject: Bug #16033: Can't return reference to class member variable from class method.
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-2356@lists.php.net to get a copy of this message
From: gabeb@canada.com Operating system: RedHat Linux 7.2, BeOS PHP version: 4.1.2 PHP Bug Type: Class/Object related Bug description: Can't return reference to class member variable from class method. References are screwy when returned from object methods. Witness the following (a script to illustrate the trouble): --------- class StringList { var $strings = array(); function &AddString( $a_string ) { $this->strings[] = $a_string; /* return a reference to newly added item */ return( $this->strings[count($this->strings)-1] ); } } $stringvar = "Hello World" ; $x = new StringList; $y = &$x->AddString( $stringvar ); /* this should change $x->strings[0], but does not! */ $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. Also, if I were to call AddString as $x->AddString("Some string"), $x->strings is no longer an array and print_r($x->strings) screams *RECURSION*! This is weird, because I am passing in the string using copy, and returning a reference to the added copy. If this is the correct behavior, I'll eat my hat. The configure line specified --without-mysql, --without-pear, and my prefix. That's all. -- Edit bug report at http://bugs.php.net/?id=16033&edit=1 -- Fixed in CVS: http://bugs.php.net/fix.php?id=16033&r=fixedcvs Fixed in release: http://bugs.php.net/fix.php?id=16033&r=alreadyfixed Need backtrace: http://bugs.php.net/fix.php?id=16033&r=needtrace Try newer version: http://bugs.php.net/fix.php?id=16033&r=oldversion Not developer issue: http://bugs.php.net/fix.php?id=16033&r=support Expected behavior: http://bugs.php.net/fix.php?id=16033&r=notwrong Not enough info: http://bugs.php.net/fix.php?id=16033&r=notenoughinfo Submitted twice: http://bugs.php.net/fix.php?id=16033&r=submittedtwice

« previous php.bugs (#2356) next »