1.2.95. Can’t Call Generator

It is not possible to call directly a generator: a generator <https://www.php.net/`generator>`_ is a method that uses the yield or yield from keyword.

Such structure shall be used directly in a foreach() structure, or with the function iterator_to_array().

<?php

function foo() {
     echo __FUNCTION__;
     yield 1;
}

// Won't display anything, even 'foo'
foo();

// displays both foo and 1
foreach(foo() as $g) {
     print $g;
}

?>

1.2.95.1. Specs

Short name

Functions/CanCallGenerator

Rulesets

All, Analyze

Exakat since

2.6.3

PHP Version

All

Severity

Minor

Time To Fix

Quick (30 mins)

Precision

High

Features

yield, yield-from, generator

Available in

Entreprise Edition, Exakat Cloud