# php5 method return reference error?

- Thread: https://externals.io/message/5411
- Original archive: https://news-web.php.net/php.internals/5411
- Started by Walter A. Boring IV on 2003-11-13T06:13:10+00:00
- Messages: 10
- 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

---

## #5411 — Walter A. Boring IV — 2003-11-13T06:13:10+00:00

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

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

---

## #5415 — Analysis & Solutions — 2003-11-13T06:52:21+00:00

URL: https://externals.io/message/5411#5415 · In reply to: #5411

Hi Walter:


> 
> "Fatal error: Only variables or references can be returned by reference
> in /home/waboring/devel/html/test.php on line 11"

This is the expected behavior.

This has been discussed in the bugs database and on this list.  I even 
started a thread on this topic today.

This group is archived in the following two places:
   http://groups.google.com/groups?group=mailing.www.php-dev
   http://marc.theaimsgroup.com/?l=php-dev

The topic is discussed in bug 24687:
   http://bugs.php.net/bug.php?id=24687

Enjoy,

--Dan

-- 
     FREE scripts that make web and database programming easier
           http://www.analysisandsolutions.com/software/
 T H E   A N A L Y S I S   A N D   S O L U T I O N S   C O M P A N Y
 4015 7th Ave #4AJ, Brooklyn NY    v: 718-854-0335   f: 718-854-0409

---

## #5437 — walt boring — 2003-11-13T18:22:31+00:00

URL: https://externals.io/message/5411#5437 · In reply to: #5415

Analysis & Solutions wrote:

>Hi Walter:
>
>
>  
>
> > […]
>
>This is the expected behavior.
>
>This has been discussed in the bugs database and on this list.  I even 
>started a thread on this topic today.
>
>This group is archived in the following two places:
>   http://groups.google.com/groups?group=mailing.www.php-dev
>   http://marc.theaimsgroup.com/?l=php-dev
>
>The topic is discussed in bug 24687:
>   http://bugs.php.net/bug.php?id=24687
>
>Enjoy,
>
>--Dan
>
>  
>
This is very sad that the only real argument in the bug against fixing 
it was
that it was hard.  Come on!  This is such a fundamental thing to be able 
to do.

function &foo() {
  return something_else();
}

How can they say this won't be fixed?  I just don't get it.  This is 
going to make a lot
of php folks upset, and make it much more painfull to upgrade to php5.  
I work on several projects,
commercial and non-commercial, that this one bug will prevent me from 
moving to php5 without
a lot of changing of classes and code, let alone the re-qualification of 
the applications due to this change.
This is just sad.

Walt

---

## #5439 — Michael Walter — 2003-11-13T18:39:17+00:00

URL: https://externals.io/message/5411#5439 · In reply to: #5411

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

---

## #5441 — Analysis & Solutions — 2003-11-13T18:43:50+00:00

URL: https://externals.io/message/5411#5441 · In reply to: #5437

Hi Walt:


>
> This is very sad that the only real argument in the bug against fixing 
> it was
> that it was hard.  Come on!  This is such a fundamental thing to be able 
> to do.
> 
> function &foo() {
>  return something_else();
> }

The thing is, as I understand it, references are referring to a variable.  
Your example doesn't have a variable to refer to.

If I misunderstand what's going on, I trust someone on the list will 
correct me.


> going to make a lot
> of php folks upset, and make it much more painfull to upgrade to php5.

But the error rests on the people who made the mistake of coding returns
by reference on items that aren't variables.

Enjoy,

--Dan

-- 
     FREE scripts that make web and database programming easier
           http://www.analysisandsolutions.com/software/
 T H E   A N A L Y S I S   A N D   S O L U T I O N S   C O M P A N Y
 4015 7th Ave #4AJ, Brooklyn NY    v: 718-854-0335   f: 718-854-0409

---

## #5443 — walt boring — 2003-11-13T18:48:26+00:00

URL: https://externals.io/message/5411#5443 · In reply to: #5441

Analysis & Solutions wrote:

>Hi Walt:
>
>
>  
>
> > […]
>
>The thing is, as I understand it, references are referring to a variable.  
>Your example doesn't have a variable to refer to.
>
>If I misunderstand what's going on, I trust someone on the list will 
>correct me.
>
>
>  
>
> > […]
>
>But the error rests on the people who made the mistake of coding returns
>by reference on items that aren't variables.
>  
>
This also fails when you do

function &foo() {
   return new bar();
}

the something_else() function in my first example could return a variable. 

function &foo() {
  return something_else();
}

function &something_else() {
    $foo = 'a';
   return $foo;
}

This also fails, and it shouldn't.

Walt

---

## #5446 — Derick Rethans — 2003-11-13T19:10:12+00:00

URL: https://externals.io/message/5411#5446 · In reply to: #5441

> The thing is, as I understand it, references are referring to a variable.
> Your example doesn't have a variable to refer to.
>
> If I misunderstand what's going on, I trust someone on the list will
> correct me.

Your assesment is right. BUT, it is indeed quite a huge BC break and
people already said that it is going to be addressed (AFAIK Zeev
mentioned that).

Derick

---

## #5447 — Derick Rethans — 2003-11-13T19:11:07+00:00

URL: https://externals.io/message/5411#5447 · In reply to: #5439

> 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?

No, it's not a reference, but an object handle which still can be passed
as reference (but it's quite unuseful to do).

> So just removing & should fix the problem, as long as you're dealing
> with objects.

Yup.

Derick

---

## #5448 — Analysis & Solutions — 2003-11-13T19:12:01+00:00

URL: https://externals.io/message/5411#5448 · In reply to: #5446

Howdy:


> 
> Your assesment is right. BUT, it is indeed quite a huge BC break and
> people already said that it is going to be addressed (AFAIK Zeev
> mentioned that).

I've heard on several occasions, some fairly recently, that it won't.

--Dan

-- 
     FREE scripts that make web and database programming easier
           http://www.analysisandsolutions.com/software/
 T H E   A N A L Y S I S   A N D   S O L U T I O N S   C O M P A N Y
 4015 7th Ave #4AJ, Brooklyn NY    v: 718-854-0335   f: 718-854-0409

---

## #5451 — walt boring — 2003-11-13T19:23:05+00:00

URL: https://externals.io/message/5411#5451 · In reply to: #5446

Derick Rethans wrote:

>
>
>  
>
> > […]
>
>Your assesment is right. BUT, it is indeed quite a huge BC break and
>people already said that it is going to be addressed (AFAIK Zeev
>mentioned that).
>
>Derick
>
>  
>
According to the bug

http://bugs.php.net/bug.php?id=24687

It won't be fixed ??

Walt
