|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #69419 Returning compatible sub generator produces a warning
Submitted: 2015-04-10 13:02 UTC Modified: 2015-04-10 16:07 UTC
From: elliotlevin at hotmail dot com Assigned: nikic (profile)
Status: Closed Package: Scripting Engine problem
PHP Version: master-Git-2015-04-10 (Git) OS:
Private report: No CVE-ID: None
 [2015-04-10 13:02 UTC] elliotlevin at hotmail dot com
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.


Test script:

function & genRefInner() {
    $var = 1;
    yield $var;

function & genRefOuter() {
    return genRefInner();

foreach(genRefOuter() as $i) {


Expected result:

Actual result:
Notice: Only variable references should be returned by reference in /in/sFR11 on line 9


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2015-04-10 14:25 UTC]
-Assigned To: +Assigned To: nikic
 [2015-04-10 15:33 UTC]
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 16:07 UTC]
Another interesting case I didn't consider before is this:


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-14 14:39 UTC]
Automatic comment on behalf of nikic
Log: Fixed bug #69419
 [2015-04-14 14:39 UTC]
-Status: Assigned +Status: Closed
 [2015-04-14 14:53 UTC]
I've split the inheritance issue into another bug:
PHP Copyright © 2001-2018 The PHP Group
All rights reserved.
Last updated: Sun Nov 19 01:31:42 2017 UTC