Fibers (PHP 8.1)
A Fiber in PHP is a callable that can stop in the middle and let the caller decide when to continue. The four blocks go from one simple pause to two Fibers taking turns.
New Fiber
A Fiber wraps a callable that can pause itself and continue later. Nothing runs at creation time, so start begins the first step on demand.
After start the Fiber either finishes or suspends, and the caller can inspect the state before moving on.
<?php $fiber = new Fiber(function (): void { echo "inside", PHP_EOL; }); var_dump($fiber->isStarted()); $fiber->start(); var_dump($fiber->isStarted()); var_dump($fiber->isTerminated());
php create.php
bool(false)
inside
bool(true)
bool(true)
The first check ran before the body, which proves that the constructor starts nothing. After start the body had run to its end, so the Fiber reports both started and terminated.
Resume with values
Start and resume can pass values in both directions. The suspend call returns whatever the caller sends on the next resume.
The final return value is read with getReturn once the Fiber has finished; start and resume hand back only what a suspend passes out.
<?php $fiber = new Fiber(function (): int { $v = Fiber::suspend("pause"); return strlen((string) $v); }); echo $fiber->start(), PHP_EOL; var_dump($fiber->resume("hello")); var_dump($fiber->getReturn());
php resume.php
pause
NULL
int(5)
The word travelled out through suspend(), the string travelled in through resume(), and the length of that string came back from getReturn().
Suspend from inside
Only code inside the Fiber can suspend it. The pause returns control to whoever called start or resume last.
This rule keeps the flow explicit, since no outside code can freeze a Fiber at an arbitrary point.
<?php $fiber = new Fiber(function (): void { echo "one", PHP_EOL; Fiber::suspend(); echo "two", PHP_EOL; }); $fiber->start(); var_dump($fiber->isSuspended()); $fiber->resume(); var_dump($fiber->isTerminated());
php suspend.php
one
bool(true)
two
bool(true)
The first line came from the body, the second shows that the Fiber really paused between the two echoes, and the last one confirms it finished after the resume.
Cooperative sketch
Two Fibers can take turns by suspending after each small step. A tiny scheduler resumes each one in order until both are done.
This style suits tasks that wait often, because waiting time can be given to the next ready task.
<?php $fiberA = new Fiber(function (): void { echo "A1 "; Fiber::suspend(); echo "A2 "; }); $fiberB = new Fiber(function (): void { echo "B1 "; Fiber::suspend(); echo "B2 "; }); $fiberA->start(); $fiberB->start(); $fiberA->resume(); $fiberB->resume(); echo PHP_EOL;
php use.php
A1 B1 A2 B2
Each start and resume printed one step of one Fiber, so the two tasks interleaved in the order the scheduler called them.
Exercises
- Print isStarted(), isSuspended() and isTerminated() before and after start.
- Send an integer into a suspended Fiber and return it doubled.
- Suspend three times and resume with a different value each time.
- Build a loop that resumes every unfinished Fiber until both are done.
- Explain why a Fiber cannot be resumed from outside the code that created it.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What happens when a Fiber object is created?
Answer
Nothing runs until start() is called
The constructor only stores the callable; start() begins the body.
-
Which value does
resume($value)hand to the Fiber?Answer
The result of the running suspend call
The paused Fiber::suspend() becomes an expression whose value is what resume() passed.
-
How is the return value of a finished Fiber read?
Answer
From
getReturn()start() and resume() hand back only what a suspend passes out; getReturn() reports the return value.
-
Who can suspend a Fiber?
Answer
Only the code running inside it
The Fiber pauses itself with Fiber::suspend(), so the flow of control stays explicit.
-
What does this program print?
<?php $fiber = new Fiber(function (): void { echo "inside", PHP_EOL; }); $fiber->start(); var_dump($fiber->isTerminated());Answer
inside / bool(true)
The body ran to its end during start, so the check afterwards reports a finished Fiber.
-
What does this program print?
<?php $fiber = new Fiber(function (): int { $v = Fiber::suspend("pause"); return strlen((string) $v); }); echo $fiber->start(), PHP_EOL; try { $fiber->getReturn(); } catch (FiberError $e) { echo get_class($e), PHP_EOL; }Answer
pause / FiberError
A suspended Fiber has no return value yet, and getReturn() refuses with a FiberError.
-
What does this program print?
<?php $fiber = new Fiber(function (): int { $v = Fiber::suspend("pause"); return strlen((string) $v); }); echo $fiber->start(), PHP_EOL; $fiber->resume("hello"); echo $fiber->getReturn();Answer
pause / 5
The word pause comes out of the suspend, and after resume the returned length is 5.
-
What does this program print?
<?php $fiber = new Fiber(function (): void { echo "one "; Fiber::suspend(); echo "two "; }); $fiber->start(); $fiber->resume();Answer
one two
The pause sits between the two echoes, and the resume lets the second one run.
-
What does this program print?
<?php $a = new Fiber(function (): void { echo "A1 "; Fiber::suspend(); echo "A2 "; }); $b = new Fiber(function (): void { echo "B1 "; Fiber::suspend(); echo "B2 "; }); $a->start(); $b->start(); $a->resume(); $b->resume();Answer
A1 B1 A2 B2
The two starts run the first step of each Fiber and the two resumes run the second step in the same order.
-
What does
isSuspended()report after a Fiber paused atFiber::suspend()?Answer
true
A Fiber that stopped at a suspend is waiting for a resume, which is exactly the suspended state.
The code on this page was run with PHP 8.5.13 (php -v) in Alpine Linux, with the commands shown above.
Article author: Arthur Isaev