php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #77489 Yield inside IF instruction
Submitted: 2019-01-19 10:54 UTC Modified: 2019-01-21 10:51 UTC
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: hlecorche at e-commit dot fr Assigned: cmb (profile)
Status: Closed Package: Scripting Engine problem
PHP Version: 7.2.14 OS: Debian GNU/Linux
Private report: No CVE-ID: None
 [2019-01-19 10:54 UTC] hlecorche at e-commit dot fr
Description:
------------
When a function has a YIELD instruction inside a IF instruction, the yield instruction is called even if the IF INSTRUCTION is not true.

Tested whith 7.0.33, 7.1.26 and 7.2.14 versions.

Test script:
---------------
function gen_one_to_three($useYield = true)
{
    $result = [];
    for ($i = 1; $i <= 3; $i++) {
        if (true === $useYield) {
            yield $i;
        } else {
            $result[] = $i;
        }
    }

    if (false === $useYield) {
        return $result;
    }
}

$return1 = gen_one_to_three(true);
echo get_class($return1)
$return2 = gen_one_to_three(false);
echo get_class($return2);

Expected result:
----------------
Generator
PHP Warning:  get_class() expects parameter 1 to be object, array given

Actual result:
--------------
Generator
Generator

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2019-01-19 11:42 UTC] cmb@php.net
-Status: Open +Status: Verified -Type: Bug +Type: Documentation Problem
 [2019-01-19 11:42 UTC] cmb@php.net
The language specification clarifies this[1]:

| Any function containing yield is a generator function.

The PHP manual could be improved in this regard, though.

[1] <https://github.com/php/php-langspec/blob/master/spec/13-functions.md#general>
 [2019-01-21 10:50 UTC] cmb@php.net
Automatic comment from SVN on behalf of cmb
Revision: http://svn.php.net/viewvc/?view=revision&amp;revision=346612
Log: Fix #77489: Yield inside IF instruction
 [2019-01-21 10:51 UTC] cmb@php.net
-Status: Verified +Status: Closed -Assigned To: +Assigned To: cmb
 [2019-01-21 10:51 UTC] cmb@php.net
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better.
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Thu May 23 20:01:28 2019 UTC