Re: php5 method return reference error?

From: Date: Thu, 13 Nov 2003 18:39:17 +0000
Subject: Re: php5 method return reference error?
References: 1  Groups: php.internals 
Request: Send a blank email to internals+get-5439@lists.php.net to get a copy of this message
Not sure I'm missing something, but I fail to see the problem: Doesn't PHP 5 use object references anyway? So function pla() { return new Foo(); } would return a reference anyway, instead of copying the object itself over? So just removing & should fix the problem, as long as you're dealing with objects. Cheers, Michael Walter A. Boring IV wrote:
Howdy, I'm playing with php5 (from cvs), and came accross a strange error that doesn't happen with php4. Maybe someone can shed some light on this for me? I get the error "Fatal error: Only variables or references can be returned by reference in /home/waboring/devel/html/test.php on line 11" Here is the php code <?php class foo {
    var $test = 0;
} class bar {
    function &get() {
        //both of these fail
        //return new foo;
        return $this->_buildOBJ();
    }
    function &_buildOBJ() {
        $obj = new foo();
        $obj->test = 'worked';
        return $obj;
    }
} $bar = new bar; $foo = $bar->get(); echo $foo->test; ?> This doesn't happen in php4. This seems to go away if I change the bar::get() method to function &get() { $obj =& $this->_buildOBJ(); return $obj; } This seems like a bug to me, since both cases the return value is a reference of an object? Thanks Walt


« previous php.internals (#5439) next »