#26439 [NEW]: Cannot directly return references

2003-11-27 Thread zhundiak at comcast dot net
From: zhundiak at comcast dot net
Operating system: NA - linux/windows
PHP version:  5.0.0b2 (beta2)
PHP Bug Type: Zend Engine 2 problem
Bug description:  Cannot directly return references

Description:

A function cannot directly return a reference produced by another function
without first assigning it to a tmp variable.
This is a specialization of bug 24687.  

Reproduce code:
---
function &getSomeReference()
{
  return getSomeOtherReference(); // Fails
}
function &getSomeReference()
{
  $tmp =& getSomeOtherReference(); // Works
  return $tmp;
}


Expected result:

It should work.

Actual result:
--
Fatal error: Only variables or references can be returned by reference


-- 
Edit bug report at http://bugs.php.net/?id=26439&edit=1
-- 
Try a CVS snapshot (php4):  http://bugs.php.net/fix.php?id=26439&r=trysnapshot4
Try a CVS snapshot (php5):  http://bugs.php.net/fix.php?id=26439&r=trysnapshot5
Fixed in CVS:   http://bugs.php.net/fix.php?id=26439&r=fixedcvs
Fixed in release:   http://bugs.php.net/fix.php?id=26439&r=alreadyfixed
Need backtrace: http://bugs.php.net/fix.php?id=26439&r=needtrace
Need Reproduce Script:  http://bugs.php.net/fix.php?id=26439&r=needscript
Try newer version:  http://bugs.php.net/fix.php?id=26439&r=oldversion
Not developer issue:http://bugs.php.net/fix.php?id=26439&r=support
Expected behavior:  http://bugs.php.net/fix.php?id=26439&r=notwrong
Not enough info:http://bugs.php.net/fix.php?id=26439&r=notenoughinfo
Submitted twice:http://bugs.php.net/fix.php?id=26439&r=submittedtwice
register_globals:   http://bugs.php.net/fix.php?id=26439&r=globals
PHP 3 support discontinued: http://bugs.php.net/fix.php?id=26439&r=php3
Daylight Savings:   http://bugs.php.net/fix.php?id=26439&r=dst
IIS Stability:  http://bugs.php.net/fix.php?id=26439&r=isapi
Install GNU Sed:http://bugs.php.net/fix.php?id=26439&r=gnused
Floating point limitations: http://bugs.php.net/fix.php?id=26439&r=float


#24687 [Com]: Fatal error: Only variables or references can be returned by reference

2003-11-27 Thread zhundiak at comcast dot net
 ID:   24687
 Comment by:   zhundiak at comcast dot net
 Reported By:  nologic at pchome dot com dot tw
 Status:   Open
 Bug Type: Zend Engine 2 problem
 Operating System: *
 PHP Version:  5CVS
 New Comment:

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.


Previous Comments:


[2003-11-19 21:26:10] 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-17 02:35:55] 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-08-16 04:14:19] 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-07-22 11:53:29] [EMAIL PROTECTED]

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-07-17 03:35:38] [EMAIL PROTECTED]

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?



The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at
http://bugs.php.net/24687

-- 
Edit this bug report at http://bugs.php.net/?id=24687&edit=1