How to use strict types with generators in PHP

1 Answer

0 votes
declare(strict_types=1);

function numbers(): \Generator {
    yield 1;
    yield 2;
    yield "3";   // Allowed: generator yields are not type‑checked
}

foreach (numbers() as $n) {
    echo $n;     // "3" is allowed here
}


/*
run:

123

*/

 



answered Aug 9 by avibootz
...