# php5 method return reference error?

- Thread: https://externals.io/message/5444
- Original archive: https://news-web.php.net/php.internals/5444
- Started by Walt Boring on 2003-11-13T01:02:45+00:00
- Messages: 1
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #5444 — Walt Boring — 2003-11-13T01:02:45+00:00

URL: https://externals.io/message/5444#5444

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
