<?php function gen() { yield; return; } function gen2() { yield; return null; } function gen3() { return; yield; } function gen4() { return; yield; } var_dump(gen()); var_dump(gen2()); var_dump(gen3()); var_dump(gen4());
$var = 24; (yield $var); return 42; } $gen = gen3(); var_dump($gen->current()); $gen->next(); var_dump($gen->getReturn()); // Return types for generators specify the return of the function, // not of the generator return value, so this code is okay function gen4() : Generator { (yield 24); return 42; } $gen = gen4(); var_dump($gen->current()); $gen->next(); var_dump($gen->getReturn()); // Has no explicit return, but implicitly return NULL at the end function gen5() { (yield 24); } $gen = gen5(); var_dump($gen->current()); $gen->next(); var_dump($gen->getReturn()); // Explicit value-less return also results in a NULL generator // return value and there is no interference with type hints function gen6() : Generator
{ return "str"; // CONST yield; } $gen = gen2(); var_dump($gen->getReturn()); function gen3($var) { return $var; // CV yield; } $gen = gen3([1, 2, 3]); var_dump($gen->getReturn()); function gen4($obj) { return $obj->prop; // VAR yield; } $gen = gen4((object) ['prop' => 321]); var_dump($gen->getReturn()); function gen5($val) { return (int) $val; // TMP yield; } $gen = gen5("42"); var_dump($gen->getReturn());