When building tools that inspect PHP code, ReflectionGenerator::getExecutingLine() helps answer a focused question: reports the current suspended source line of a generator.
Reflection is useful in framework tooling, dependency injection, testing, code inspection and debugging.
What Is ReflectionGenerator::getExecutingLine()?
Reports the current suspended source line of a generator.
Its method signature is:
public function getExecutingLine(): int
This method has no required arguments.
Return value: int
Example 1: Inspect the basic behavior
Start with a self-contained PHP declaration and inspect the method result.
<?php
declare(strict_types=1);
function makeSequence(): Generator {
yield "first" => 10;
yield from ["next" => 20];
}
$generator = makeSequence();
$generator->rewind();
$generatorReflection = new ReflectionGenerator($generator);
$result = $generatorReflection->getExecutingLine();
var_dump($result);
Output:
int(6)
Explanation: Reports the current suspended source line of a generator.
Example 2: Compare a second case
Next, change the reflected declaration or parameter to observe a related situation.
<?php
declare(strict_types=1);
function makeSequence(): Generator {
yield "first" => 11;
yield from ["next" => 21];
}
$generator = makeSequence();
$generator->rewind();
$generatorReflection = new ReflectionGenerator($generator);
$result = $generatorReflection->getExecutingLine();
var_dump($result);
// Check the returned value type before using the result.
echo "\nResult type: " . get_debug_type($result);
Output:
int(6)
Result type: int
Explanation: Line numbers are source-dependent and change when the code changes.
Practical Example: Apply the operation in context
This final case shows how the same API can be used when inspecting application definitions.
<?php
declare(strict_types=1);
function makeSequence(): Generator {
yield "first" => 12;
yield from ["next" => 22];
}
$generator = makeSequence();
$generator->rewind();
$generatorReflection = new ReflectionGenerator($generator);
$result = $generatorReflection->getExecutingLine();
var_dump($result);
// Use the reflection result in a simple decision.
if (is_bool($result)) {
echo "\nStatus: " . ($result ? "yes" : "no");
} elseif (is_array($result)) {
echo "\nEntries: " . count($result);
} elseif ($result === null) {
echo "\nNo value returned";
} else {
echo "\nReflection result available";
}
Output:
int(6)
Reflection result available
Explanation: getTrace supplies stack information instead.
Important Behavior and Edge Cases
- Line numbers are source-dependent and change when the code changes.
- getTrace supplies stack information instead.
- Reflection describes declarations and runtime state; requesting metadata does not automatically validate or sanitize application input.
Comparison With Related Approaches
| Approach | When to Use It |
|---|---|
ReflectionGenerator::getExecutingLine() | Reports the current suspended source line of a generator. |
Related API | getTrace supplies stack information instead. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Ignoring this condition: Line numbers are source-dependent and change when the code changes. | Handle the specific limitation: Line numbers are source-dependent and change when the code changes. |
| Confusing this method with Related API | getTrace supplies stack information instead. |
Key Takeaways
- Reports the current suspended source line of a generator.
- Line numbers are source-dependent and change when the code changes.
- getTrace supplies stack information instead.
Use ReflectionGenerator::getExecutingLine() when you need the precise metadata or behavior demonstrated above. Keep the target declaration and relevant PHP version in mind when adapting the examples.