In addition to the note of "montoriusz at gmail dot com": https://www.php.net/manual/en/language.generators.overview.php#119275
"If you need to perform some action when the function is called and before the result is used, you'll have to wrap your generator in another function."
You can use Generator::rewind instead (https://www.php.net/manual/en/generator.rewind.php)
Sample code:
<?php
/** function/generator definition **/
echo "calling gen()...\n";
$result = gen();
$result->rewind();
echo "gen() was called\n";
/** iteration **/
?>