Fibers — Cooperative Concurrency (PHP 8.1)
TL;DR
PHP 8.1 Fibers enable cooperative multitasking — suspending execution at yield points and resuming later — the foundation for async PHP frameworks without OS threads.
Explanation
Fiber::create($callback) creates a fiber. $fiber->start($args) begins execution. Fiber::suspend($value) pauses and returns value to caller. $fiber->resume($value) continues from the suspension point. Fibers are single-threaded — not parallel. They enable: event-loop integration (ReactPHP, Revolt, Swoole), cooperative multitasking, coroutine-style async code without callbacks. The Revolt event loop uses Fibers to implement async/await-like patterns. Unlike generators, Fibers can suspend from any call depth (generators only yield from the generator function itself). This is the PHP equivalent of Python's asyncio coroutines.
Common Misconception
✗ Fibers provide parallelism — they're cooperative concurrency (single-threaded). Multiple fibers take turns, not run simultaneously. Use pcntl_fork or worker_threads for actual parallelism.
Why It Matters
Fibers enable proper async programming in PHP without callback hell — the foundation for the next generation of PHP async frameworks and database drivers.
Common Mistakes
- Expecting Fibers to use multiple CPU cores — they're single-threaded.
- Using Fibers without an event loop — fibers alone don't help without something to schedule them.
- Confusing with PHP generators — fibers can suspend from any call depth.
Code Examples
✗ Vulnerable
// Generators only yield from generator function:
function gen() {
yield doSomething(); // Only works at generator level
}
✓ Fixed
$fiber = new Fiber(function(): void {
$value = Fiber::suspend('first');
echo 'Resumed with: ' . $value . PHP_EOL;
});
$yielded = $fiber->start(); // 'first'
$fiber->resume('hello'); // 'Resumed with: hello'
// Real use: Revolt event loop
// use Revolt\EventLoop;
// EventLoop::run();
Tags
🤝 Adopt this term
£79/year · your link shown here
Added
23 Mar 2026
Views
53
🤖 AI Guestbook educational data only
|
|
Last 30 days
Agents 1
No pings yesterday
ChatGPT 16
Perplexity 11
Amazonbot 6
Google 6
Unknown AI 4
Ahrefs 1
Also referenced
How they use it
crawler 39
crawler_json 4
pre-tracking 1
Related categories
⚡
DEV INTEL
Tools & Severity
🔵 Info
⚙ Fix effort: High
⚡ Quick Fix
Use Fibers via Revolt/ReactPHP event loop rather than directly. Revolt's async/await API wraps Fibers in a usable interface.
📦 Applies To
PHP 8.1+
cli
queue-worker
ReactPHP
Swoole
Revolt
🔗 Prerequisites
🔍 Detection Hints
new Fiber|Fiber::suspend
Auto-detectable:
✗ No
🤖 AI Agent
Confidence: Low
False Positives: High
✗ Manual fix
Fix: High
Context: File
Tests: Update