php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #16033 Can't return reference to class member variable from class method.
Submitted: 2002-03-13 02:45 UTC Modified: 2002-03-13 12:42 UTC
Votes:3
Avg. Score:3.3 ± 1.2
Reproduced:3 of 3 (100.0%)
Same Version:2 (66.7%)
Same OS:1 (33.3%)
From: gabeb at canada dot com Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: 4.1.2 OS: RedHat Linux 7.2, BeOS
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: gabeb at canada dot com
New email:
PHP Version: OS:

 

 [2002-03-13 02:45 UTC] gabeb at canada dot com
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.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-03-13 12:42 UTC] gabeb at canada dot com
Whoops, I messed up. Sorry!
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Dec 22 01:01:30 2024 UTC