PHP has traditionally been associated with a straightforward execution model: receive a request, execute application code, produce a response, and finish. This model remains effective for a large proportion of web applications, but it is not the only way PHP can execute code.
Since PHP 8.1, Fibers have provided a mechanism for suspending and resuming functions while preserving their execution stacks. They are a low-level building block for cooperative concurrency, rather than a complete asynchronous programming framework.
That distinction matters. Fibers make certain execution patterns possible, but they do not automatically make blocking operations non-blocking or cause PHP code to execute simultaneously on multiple CPU cores.
The Difference Between a Fiber and a Generator
PHP generators already allow functions to pause execution using yield. They are useful for iteration, lazy data generation, and memory-efficient processing.
A Fiber goes further because it preserves an entire call stack. A deeply nested function can suspend the currently executing Fiber without requiring every intermediate function to become a generator.
This capability is particularly valuable to libraries implementing asynchronous abstractions. Application code can retain a relatively conventional structure while a scheduler controls when execution resumes.
Starting and Suspending a Fiber
The simplest demonstration requires only the built-in Fiber class.
<?php
declare(strict_types=1);
$fiber = new Fiber(function (): string {
echo "Task started\n";
$message = Fiber::suspend('waiting');
echo "Resumed with: {$message}\n";
return 'complete';
});
$status = $fiber->start();
echo "Scheduler received: {$status}\n";
$fiber->resume('continue');
echo "Result: ", $fiber->getReturn(), "\n";Expected output:
Task started
Scheduler received: waiting
Resumed with: continue
Result: completeThe call to start() executes the Fiber until it suspends. The value supplied to Fiber::suspend() is returned to the caller of start(). Later, resume() continues execution, and its argument becomes the return value of the suspended call.
Once the Fiber terminates, getReturn() retrieves its final return value. Calling resume() on a Fiber that is not suspended is an error.
Building a Small Cooperative Scheduler
To understand the scheduling model, consider three tasks that voluntarily yield control after completing a unit of work.
The following example implements round-robin scheduling without external dependencies.
<?php
declare(strict_types=1);
function makeTask(string $name, int $steps): Fiber
{
return new Fiber(
function () use ($name, $steps): void {
for ($step = 1; $step <= $steps; $step++) {
echo "{$name}: step {$step}\n";
Fiber::suspend();
}
}
);
}
$tasks = [
makeTask('Import', 3),
makeTask('Report', 2),
makeTask('Cleanup', 2),
];
while ($tasks !== []) {
foreach ($tasks as $index => $fiber) {
if (!$fiber->isStarted()) {
$fiber->start();
} elseif ($fiber->isSuspended()) {
$fiber->resume();
}
if ($fiber->isTerminated()) {
unset($tasks[$index]);
}
}
}The scheduler advances each task by one cooperative step before moving to the next. The tasks share a single execution thread and do not run in parallel.
Notice that the Fiber suspends even after its final loop iteration. The scheduler must resume it once more so the function can return and the Fiber can terminate.
Why This Is Not Automatically Asynchronous I/O
A common misconception is that wrapping a blocking database query or HTTP request inside a Fiber makes it asynchronous.
It does not. If a Fiber executes a blocking operation, the PHP thread remains blocked until that operation completes. Other Fibers cannot progress during that interval merely because they exist.
Useful asynchronous I/O requires additional infrastructure: non-blocking operations, readiness notifications, timers, and a scheduler capable of resuming Fibers when their work can continue.
Libraries and event-loop implementations provide these facilities. Fibers supply the execution-control mechanism, not the entire I/O system.
Exceptions, Cancellation, and Lifecycle Management
Production schedulers need to handle more than successful suspension and resumption.
A Fiber can throw an exception. An exception escaping from a Fiber propagates to the caller of start() or resume(). Without appropriate handling, one failed task may interrupt the entire scheduler.
PHP also provides Fiber::throw(), allowing an exception to be injected into a suspended Fiber. This can support cancellation mechanisms, although the surrounding application must define how cleanup and resource ownership work.
A robust implementation should account for task cancellation, deadlines, exception propagation, resource cleanup, and the risk of tasks that never voluntarily suspend.
When Should Developers Use Fibers Directly?
Most application developers do not need to implement their own scheduler. Direct Fiber usage is most appropriate when building runtime libraries, experimental concurrency tools, or specialized execution frameworks.
For ordinary applications, a maintained asynchronous library may provide a safer abstraction. For CPU-intensive parallel work, Fibers are not a replacement for processes or other genuine parallel-execution mechanisms.
The practical value of Fibers is that they separate execution suspension from the structure of the functions being executed. That capability expands what PHP libraries can offer without requiring every application to adopt a new programming language or runtime.

















![PHP #[Override]: Catching Broken Inheritance Contracts Before Production](https://phpmagazine.net/wp-content/uploads/2026/11/php-override-218x150.png)






