Generators

The ReflectionGenerator class has been added for introspecting Generator objects:

function xrange(int $start, int $end, int $step = 1) {
    for ($i = $start; $i <= $end; $i += $step) {
        yield $i;
    }
}

$generator = new ReflectionGenerator(xrange(1, 2, 1));

var_dump($generator->getExecutingGenerator());
var_dump($generator->getExecutingFile());
var_dump($generator->getExecutingLine());
var_dump($generator->getTrace());
var_dump($generator->getFunction());
var_dump($generator->getThis());

Executing the code shown above will print the output shown below:

array(0) {
}
int(3)
string(17) "example.php"
object(Generator)#2 (0) {
}
object(ReflectionFunction)#3 (1) {
  ["name"]=>
  string(6) "xrange"
}
NULL

The getExecutingGenerator() method returns the executing Generator object, getExecutingFile() and getExecutingLine() return the full path and name of the file in which as well as the number of the line on which the executing generator was created, getFunction() returns an ReflectionFunctionAbstract instance that represents the generator function, getTrace() returns the trace of the currently executing generator, and getThis() returns the $this reference the generator has access to if it was declared in a class.