When building tools that inspect PHP code, ReflectionFiber::getExecutingLine() helps answer a focused question: returns the source-code line where the fiber is suspended.
Reflection is useful in framework tooling, dependency injection, testing, code inspection and debugging.
What Is ReflectionFiber::getExecutingLine()?
Returns the source-code line where the fiber is suspended.
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);
$fiber = new Fiber(static function (): void {
$count = 1;
Fiber::suspend($count);
});
$fiber->start();
$fiberReflection = new ReflectionFiber($fiber);
$result = $fiberReflection->getExecutingLine();
var_dump($result);
Output:
int(7)
Explanation: Returns the source-code line where the fiber is suspended.
Example 2: Compare a second case
Next, change the reflected declaration or parameter to observe a related situation.
<?php
declare(strict_types=1);
$fiber = new Fiber(static function (): void {
$count = 2;
Fiber::suspend($count);
});
$fiber->start();
$fiberReflection = new ReflectionFiber($fiber);
$result = $fiberReflection->getExecutingLine();
var_dump($result);
// Check the returned value type before using the result.
echo "\nResult type: " . get_debug_type($result);
Output:
int(7)
Result type: int
Explanation: Line numbers depend on the actual source file, so do not hard-code them.
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);
$fiber = new Fiber(static function (): void {
$count = 3;
Fiber::suspend($count);
});
$fiber->start();
$fiberReflection = new ReflectionFiber($fiber);
$result = $fiberReflection->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(7)
Reflection result available
Explanation: getTrace returns a stack trace with more context.
Important Behavior and Edge Cases
- Line numbers depend on the actual source file, so do not hard-code them.
- getTrace returns a stack trace with more context.
- 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 |
|---|---|
ReflectionFiber::getExecutingLine() | Returns the source-code line where the fiber is suspended. |
Related API | getTrace returns a stack trace with more context. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Ignoring this condition: Line numbers depend on the actual source file, so do not hard-code them. | Handle the specific limitation: Line numbers depend on the actual source file, so do not hard-code them. |
| Confusing this method with Related API | getTrace returns a stack trace with more context. |
Key Takeaways
- Returns the source-code line where the fiber is suspended.
- Line numbers depend on the actual source file, so do not hard-code them.
- getTrace returns a stack trace with more context.
Use ReflectionFiber::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.