Fibers (PHP 8.1)

Contents
New Fiber
Resume with values
Suspend from inside
Cooperative sketch

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.

$fiber = new Fiber(function (): void { echo "inside"; }); $fiber->start(); var_dump($fiber->isTerminated());

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 comes back from start or from the last resume that lets the Fiber finish.

$fiber = new Fiber(function (): int { $v = Fiber::suspend("pause"); return strlen((string) $v); }); echo $fiber->start(); echo $fiber->resume("hello");

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.

$fiber = new Fiber(function (): void { echo "one"; Fiber::suspend(); echo "two"; }); $fiber->start(); $fiber->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.

$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();

Next: Attributes via reflection

Article author: Arthur Isaev

Related articles
Development with PHP
PHP course: zero to hero in 50 lessons
What is PHP and where it runs
Install PHP 8.5 and run your first command
Your first PHP script
Comments in PHP
Variables and constants
Types in PHP
Strings in PHP
Numbers in PHP
Operators
if and else
switch and match
Ternary, null coalescing and nullsafe
Loops
Arrays
Associative arrays
Functions
Variable scope
include and require
Namespaces
Composer and autoloading
Everyday string functions
Dates and time
JSON in PHP
Reading and writing files
Errors: read, reproduce, fix
Classes and objects
Constructors and promotion
Inheritance
Interfaces and traits
Enums
Magic methods
Attributes
Exceptions
Strict types
Closures and arrow functions
Generators with yield
Forms: GET and POST
HTTP: headers and status codes
Cookies
Sessions
Login with sessions
JSON REST endpoint
Database with PDO
Uploads and .htaccess
Lint like a pro
Clean functions
Debugging
Modern PHP tour: 5 to 8.4
PHP 8.5 for busy developers
Hero roadmap: the whole course on one page
Install PHP 8.5 on Windows 11
Install PHP 8.5 on Ubuntu
Install PHP 8.5 on Rocky Linux
Install PHP 8.5 on macOS
Install PHP 8.5 on FreeBSD
PHP 8.5 in Docker

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.com
Write to info@aredel.com if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: