php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #24687 Fatal error: Only variables or references can be returned by reference
Submitted: 2003-07-16 19:00 UTC Modified: 2003-12-31 11:46 UTC
Votes:25
Avg. Score:4.6 ± 0.9
Reproduced:24 of 24 (100.0%)
Same Version:14 (58.3%)
Same OS:13 (54.2%)
From: nologic at pchome dot com dot tw Assigned: zeev (profile)
Status: Closed Package: Scripting Engine problem
PHP Version: 5CVS OS: *
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: nologic at pchome dot com dot tw
New email:
PHP Version: OS:

 

 [2003-07-16 19:00 UTC] nologic at pchome dot com dot tw
Description:
------------
apache_2.0.47-win32-x86-no_ssl
XOOPS203 ( http://www.xoops.org )

For install:

    php5-win32-200307162230 => Fatal Error !
    php4.3.2                => succeeded !


sorry...my English is poor.
    


Reproduce code:
---------------
class TextSanitizer
{
function &htmlSpecialChars($text)
 {
line 96 -> return preg_replace("/&/i", '&', htmlspecialchars($text, ENT_QUOTES));
 }
}

Expected result:
----------------
Fatal error: Only variables or references can be returned by reference in D:\www\xoops2\html\install\class\textsanitizer.php on line 96


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-07-17 03:11 UTC] derick@php.net
Actually this is not a bug, but just another BC break, but a pretty huge one. I think it's too much of a problem not to address it.
 [2003-07-17 03:23 UTC] zeev@php.net
It never really worked (caused memory corruption).
Unlikely to be changed, since it doesn't make sense, but we'll see.
 [2003-07-17 03:35 UTC] derick@php.net
If you do it like this it works:

class TextSanitizer
{
 function &htmlSpecialChars($text) {
  $foo = preg_replace("/&/i", '&', htmlspecialchars($text, ENT_QUOTES));
  return $foo;
 }
}

So would it really be *that* hard to make it work?
 [2003-07-22 11:53 UTC] zeev@php.net
Yes, it is quite complicated.  You can only return variables by reference, a function's return value is not something we can 'connect' to...
 [2003-08-16 04:14 UTC] jan at horde dot org
Any idea yet if this will be fixed/addressed or should we start converting scripts to not use referenced return type or build variables that get returned?
 [2003-11-17 02:35 UTC] mag at alcormizar dot cjb dot net
I agree with Zeev on this one, this shouldn't be fixed. People will need to learn to write good code one day or the other, and returning a reference like that is VERY bad practice (try to do that in C++!). Even returning a reference to a local variable like this example should produce an error :

class foo
{
    function &bar()
    {
        $whatever = 5;
        return $whatever;
    }
}

because $whatever should not exist anymore after function bar return (according to correct scope rules), thus this reference is not referencing anything anymore... Instead of always asking to fix bad programming practice people should learn the correct way to do it. This is my opinion.
 [2003-11-19 21:26 UTC] kain at kuht dot it
and this?

Fatal error: Only variables or references can be returned by reference in /usr/lib/php/DB/common.php on line 766

function &query($query, $params = array()) {
 if (sizeof($params) > 0) {
  $sth = $this->prepare($query);
   if (DB::isError($sth)) {
    return $sth;
   }
  $ret = $this->execute($sth, $params);
  $this->freePrepared($sth);
  return $ret;
 } else {
  $result = $this->simpleQuery($query);
   if (DB::isError($result) || $result === DB_OK) {
    return $result;
   } else {
-->766 return new DB_result($this, $result);
   }
  }
 }

running on Apache/2.0.48 (Trustix Secure Linux/Linux) PHP/5.0.0b2
 [2003-11-27 14:53 UTC] zhundiak at comcast dot net
This bug pretty much rules out backwards compatibility with most php4 OOP programs.  It's quite silly to say that returning a reference by calling another function that returns a reference is "bad" programing practice.  And by the way, C++ has no problem with returning pointers.

Just to be clear:

function &getHomeTeam()
{
  return $this->getTeam(TEAM_TYPE_HOME);
}
Really should work.
 [2003-11-28 23:38 UTC] sniper@php.net
Zeev, please, either fix this or set the status to 'wont fix'.

 [2003-12-31 11:46 UTC] andi@php.net
This bug has been fixed in CVS.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.
 
Thank you for the report, and for helping us make PHP better.


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Nov 21 18:01:29 2024 UTC