php5 method return reference error?

php.internals

Walt Boring

22 years ago
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

Daniel Convissor

22 years ago
Hi Walter: On Wed, Nov 12, 2003 at 10:13:10PM -0800, Walter A. Boring IV wrote:
> > "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

Walt Boring

22 years ago
Analysis & Solutions wrote:
>Hi Walter: > >On Wed, Nov 12, 2003 at 10:13:10PM -0800, Walter A. Boring IV wrote: > > >>"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 > > >
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

Daniel Convissor

22 years ago
Hi Walt: On Thu, Nov 13, 2003 at 10:22:31AM -0800, walt boring wrote:
> > 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

Walt Boring

22 years ago
Analysis & Solutions wrote:
>Hi Walt: > >On Thu, Nov 13, 2003 at 10:22:31AM -0800, walt boring wrote: > > >>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. > >
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

Derick Rethans

22 years ago
On Thu, 13 Nov 2003, Analysis & Solutions wrote:
> 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

Daniel Convissor

22 years ago
Howdy: On Thu, Nov 13, 2003 at 08:10:12PM +0100, 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).
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

Walt Boring

22 years ago
Derick Rethans wrote:
>On Thu, 13 Nov 2003, Analysis & Solutions wrote: > > > >>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 > > >
According to the bug http://bugs.php.net/bug.php?id=24687 It won't be fixed ?? Walt

Michael Walter

22 years ago
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:

Derick Rethans

22 years ago
On Thu, 13 Nov 2003, Michael Walter wrote:
> 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