← CodeClarityLab Home
Browse by Category
+ added · updated 7d
← Back to glossary

Fibers — Cooperative Concurrency (PHP 8.1)

php PHP 8.1+ Advanced

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

Added 23 Mar 2026
Views 53
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings W 0 pings T 0 pings F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 0 pings S 0 pings S 0 pings M 1 ping T 1 ping W 1 ping T 7 pings F 2 pings S 3 pings S 1 ping M 0 pings T 0 pings W 1 ping T
No pings yesterday
ChatGPT 16 Perplexity 11 Amazonbot 6 Google 6 Unknown AI 4 Ahrefs 1
crawler 39 crawler_json 4 pre-tracking 1
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

✓ schema.org compliant