ID:               16033
 Updated by:       [EMAIL PROTECTED]
-Summary:          Can't return reference to class member variable from
                   class method.
 Reported By:      [EMAIL PROTECTED]
-Status:           Open
+Status:           Bogus
 Bug Type:         Class/Object related
 Operating System: RedHat Linux 7.2, BeOS
 PHP Version:      4.1.2
 New Comment:

Whoops, I messed up. Sorry!


Previous Comments:
------------------------------------------------------------------------

[2002-03-13 02:45:24] [EMAIL PROTECTED]

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 this bug report at http://bugs.php.net/?id=16033&edit=1

Reply via email to