php.net |  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
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)

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-04-10 14:25 UTC] laruence@php.net
-Assigned To: +Assigned To: nikic
 [2015-04-10 15:33 UTC] 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 16:07 UTC] 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-14 14:39 UTC] 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-14 14:39 UTC] nikic@php.net
-Status: Assigned +Status: Closed
 [2015-04-14 14:53 UTC] nikic@php.net
I've split the inheritance issue into another bug: https://bugs.php.net/bug.php?id=69447.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Nov 21 08:01:29 2024 UTC