Bug #52825 [NEW]: return by reference with ternary operator causes notice "Only variable..."

From: Date: Mon, 13 Sep 2010 04:45:41 +0000
Subject: Bug #52825 [NEW]: return by reference with ternary operator causes notice "Only variable..."
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-154284@lists.php.net to get a copy of this message
From:             
Operating system: Windows XP SP3
PHP version:      5.3.3
Package:          *General Issues
Bug Type:         Bug
Bug description:return by reference with ternary operator causes notice "Only variable..."


Description:
------------
I don't know if it's a bug, or that's how it should be, but it would be
logical to assume that a ternary operator is equal to an IF block:

return $a ? $b : $c;
// ==
if ($a) return $b; else return $c;

I believe that's how it is in C. However when returning a reference, the
first version produces a notice:
"Notice: Only variable references should be returned by reference"

It's hard to tell if this is normal behaviour or not, but it's confusing.
If this is normal functionality, then I think this should be in
documentation, at least in a form of a comment.

Test script:
---------------
class test {
	private $a = array();
	private $b = array();

	function &getA($key) {
		return $key > 0 ? $this->a[$key] : $this->b[$key]; 
		// the following line gives no notice
		// if ($key > 0) return $this->a[$key]; else return $this->b[$key];
	}
}

$a = new test;
$b = &$a->getA('1');

Expected result:
----------------
Notice: Undefined index: 1 in - on line 9

Notice: Only variable references should be returned by reference in - on
line 9



Actual result:
--------------
Notice: Undefined index: 1 in - on line 9



-- 
Edit bug report at http://bugs.php.net/bug.php?id=52825&edit=1
-- 
Try a snapshot (PHP 5.2):            http://bugs.php.net/fix.php?id=52825&r=trysnapshot52
Try a snapshot (PHP 5.3):            http://bugs.php.net/fix.php?id=52825&r=trysnapshot53
Try a snapshot (trunk):              http://bugs.php.net/fix.php?id=52825&r=trysnapshottrunk
Fixed in SVN:                        http://bugs.php.net/fix.php?id=52825&r=fixed
Fixed in SVN and need be documented: http://bugs.php.net/fix.php?id=52825&r=needdocs
Fixed in release:                    http://bugs.php.net/fix.php?id=52825&r=alreadyfixed
Need backtrace:                      http://bugs.php.net/fix.php?id=52825&r=needtrace
Need Reproduce Script:               http://bugs.php.net/fix.php?id=52825&r=needscript
Try newer version:                   http://bugs.php.net/fix.php?id=52825&r=oldversion
Not developer issue:                 http://bugs.php.net/fix.php?id=52825&r=support
Expected behavior:                   http://bugs.php.net/fix.php?id=52825&r=notwrong
Not enough info:                     http://bugs.php.net/fix.php?id=52825&r=notenoughinfo
Submitted twice:                     http://bugs.php.net/fix.php?id=52825&r=submittedtwice
register_globals:                    http://bugs.php.net/fix.php?id=52825&r=globals
PHP 4 support discontinued:          http://bugs.php.net/fix.php?id=52825&r=php4
Daylight Savings:                    http://bugs.php.net/fix.php?id=52825&r=dst
IIS Stability:                       http://bugs.php.net/fix.php?id=52825&r=isapi
Install GNU Sed:                     http://bugs.php.net/fix.php?id=52825&r=gnused
Floating point limitations:          http://bugs.php.net/fix.php?id=52825&r=float
No Zend Extensions:                  http://bugs.php.net/fix.php?id=52825&r=nozend
MySQL Configuration Error:           http://bugs.php.net/fix.php?id=52825&r=mysqlcfg



Thread (4 messages)

« previous php.bugs (#154284) next »