function test() { $x = new X(); $f = $x->gen(function ($x) { var_dump(get_class($x)); }); foreach ($f() as $e) { var_dump($e); } }
<?php trait T { public static function gen() { static $x; (yield ++$x); (yield 2); (yield ++$x); } } class X { use T; } class Y extends X { } $g = X::gen(); foreach ($g as $i) { var_dump($i); }