Fibers introduced in PHP 8.1 provide explicit suspension and resumption of a function call stack.
The examples below begin with the core behavior, then move to an application-oriented use.
How PHP 8.1 Fibers: Suspending and Resuming Work Works
Fibers introduced in PHP 8.1 provide explicit suspension and resumption of a function call stack.
Example 1: Basic Usage
This example uses a small input to make the main behavior visible.
<?php
$fiber = new Fiber(function (): void { echo "begin "; Fiber::suspend(); echo "end"; });
$fiber->start();
$fiber->resume();
Output:
begin end
Fibers introduced in PHP 8.1 provide explicit suspension and resumption of a function call stack.
Example 2: A Different Input
A different set of values shows the same operation in another situation.
<?php
$fiber = new Fiber(function (): string { $value = Fiber::suspend("waiting"); return strtoupper($value); });
echo $fiber->start();
$fiber->resume("ready");
echo $fiber->getReturn();
Output:
waitingREADY
A fiber must be suspended before resume() and has no automatic scheduler; blocking I/O remains blocking.
Practical Example: Applying the Feature
This example shows an application-style use of the feature, with its surrounding setup visible.
<?php
$fiber = new Fiber(function (): string { $response = Fiber::suspend("Need a value"); return "Got: " . $response; });
echo $fiber->start() . "\n";
$fiber->resume("approved");
echo $fiber->getReturn();
Output:
Need a value
Got: approved
The code is shown for learning. Adjust the class names, inputs and error handling to suit your application.
Important Behavior and Edge Cases
- A fiber must be suspended before resume() and has no automatic scheduler; blocking I/O remains blocking.
- Fibers introduced in PHP 8.1 provide explicit suspension and resumption of a function call stack.
- Observe differences in empty inputs, invalid states and optional extension availability where relevant.
Comparison With Related Approaches
| Approach | Important Difference |
|---|---|
PHP 8.1 Fibers: Suspending and Resuming Work | Fibers introduced in PHP 8.1 provide explicit suspension and resumption of a function call stack. |
| Alternative approach | Generators suspend at yield points within a generator, while fibers can suspend nested call stacks. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
Ignoring the documented state or precondition of PHP 8.1 Fibers: Suspending and Resuming Work | A fiber must be suspended before resume() and has no automatic scheduler; blocking I/O remains blocking. |
| Expecting a different type of result | Read the declared return type and distinguish returned values from changes to the source object. |
| Assuming a related API works identically | Generators suspend at yield points within a generator, while fibers can suspend nested call stacks. |
Key Takeaways
- Fibers introduced in PHP 8.1 provide explicit suspension and resumption of a function call stack.
- A fiber must be suspended before resume() and has no automatic scheduler; blocking I/O remains blocking.
- Use the example that matches the values, runtime version and dependencies in your application.
PHP 8.1 Fibers: Suspending and Resuming Work is useful when its behavior matches the data structure and lifecycle of your PHP application. Choose it with a clear understanding of its preconditions and returned values.