Bug #69419 [Com]: Returning compatible sub generator produces a warning
| From: | nikic@php.net | Date: | Tue, 14 Apr 2015 14:53:45 +0000 |
| Subject: | Bug #69419 [Com]: Returning compatible sub generator produces a warning | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-192047@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=69419&edit=1
ID: 69419
Comment by: nikic@php.net
Reported by: elliotlevin at hotmail dot com
Summary: Returning compatible sub generator produces a
warning
Status: Closed
Type: Bug
Package: Scripting Engine problem
PHP Version: master-Git-2015-04-10 (Git)
Assigned To: nikic
Block user comment: N
Private report: N
New Comment:
I've split the inheritance issue into another bug: https://bugs.php.net/bug.php?id=69447.
Previous Comments:
------------------------------------------------------------------------
[2015-04-14 14:39:28] nikic@php.net
Automatic comment on behalf of nikic
Revision: http://git.php.net/?p=php-src.git;a=commit;h=95650d0942c99db8c247797d2ce708a586298ae9
Log: Fixed bug #69419
------------------------------------------------------------------------
[2015-04-10 16:07:58] nikic@php.net
Another interesting case I didn't consider before is this:
<?php
class A {
public function &gen() { yield; }
}
class B extends A {
public function gen() { yield; }
}
?>
Currently this throws the usual LSP strict standards notice, because by-reference returns are
covariant.
Intuitively this seem correct to me as well, as you can iterate a by-ref generator by-val, but
can't iterate a by-val generator by-ref.
------------------------------------------------------------------------
[2015-04-10 15:33:38] nikic@php.net
The behavior in PHP 7 is correct, I'll fix it to throw a strict standards notice in 5.x as
well.
------------------------------------------------------------------------
[2015-04-10 13:02:37] elliotlevin at hotmail dot com
Description:
------------
When generators were implemented, a generator function could return another generator even if it was
a return by reference generator.
In PHP7 this now produces a warning because it is not a variable.
See http://3v4l.org/sFR11#vphp7@20141101
Test script:
---------------
<?php
function & genRefInner() {
$var = 1;
yield $var;
}
function & genRefOuter() {
return genRefInner();
}
foreach(genRefOuter() as $i) {
var_dump($i);
}
?>
Expected result:
----------------
int(1)
Actual result:
--------------
Notice: Only variable references should be returned by reference in /in/sFR11 on line 9
int(1)
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=69419&edit=1