PHP theorytheory 0/50 · 0%
Concurrency · hard

40. Fibers

Cooperative multitasking primitives.

Fibers, introduced in PHP 8.1, are a low-level primitive for cooperative multitasking: a Fiber wraps a callable and can suspend its execution mid-function with Fiber::suspend(), later resumed with $fiber->resume(). Unlike generators, fibers can suspend from nested function calls, not just the top-level function.

Fibers are the building block that async libraries (like ReactPHP or Amp) use internally to implement coroutines and non-blocking I/O without changing PHP's fundamentally single-threaded execution model per process.

Check your understanding

  1. 1. What PHP version introduced Fibers?

  2. 2. How do Fibers differ from generators regarding suspension?