User Tools

Site Tools


advanced_concepts:php_fibers

1. Fibers — Cooperative Concurrency With Predictable Flow

Fibers are the deepest conceptual shift PHP has made in years.
They’re not “faster loops” or “threads”.
They’re a new way to express pausable execution
— a quiet, controlled form of concurrency that keeps PHP’s single‑threaded mental model intact.

Fibers introduce a way to pause and resume execution without breaking PHP’s single‑threaded nature.
They give us concurrency without chaos
— a controlled, cooperative model where the developer decides when execution yields.

They reflect a broader shift in modern PHP:
asynchronous behavior should be explicit, predictable, and safe.


1. What Fibers Are

A Fiber is a small, self‑contained unit of execution that can be paused (suspend) and resumed (resume) manually.

$fiber = new Fiber(function () {
    $value = Fiber::suspend('waiting');
    return "resumed with $value";
});
 
$result = $fiber->start();   // 'waiting'
$result = $fiber->resume('data'); // 'resumed with data'

Nothing happens automatically.
Nothing runs in parallel.
Everything is explicit.


2. Why Modern PHP Uses Fibers

Fibers reduce:

  • callback pyramids
  • deeply nested promises
  • unreadable asynchronous code
  • framework‑specific hacks
  • accidental blocking

They give frameworks a foundation for async I/O
without changing PHP’s core execution model.


3. The Mental Model

A Fiber is a function we can pause.

- Not a thread.
- Not a process.
- Not parallel execution.

The key idea:
Fibers give us concurrency without losing the simplicity of sequential code.

We write code that looks synchronous,
but behaves cooperatively.


4. A Simple Example

Without Fibers (callback style):

fetchData(function ($data) {
    process($data);
});

With Fibers (cooperative style):

$fiber = new Fiber(function () {
    $data = Fiber::suspend();
    process($data);
});
 
$fiber->start();
$fiber->resume(fetchData());

The flow becomes readable.
The behavior becomes predictable.


5. Where Fibers Shine

Fibers are not for everyday PHP.
They are for frameworks and libraries that need structured concurrency.

They shine in:

  • async I/O
  • event loops
  • task schedulers
  • cooperative multitasking
  • coroutine‑based frameworks

Most developers will use Fibers indirectly
— through libraries that build on top of them.


6. When Not to Use Fibers

Avoid them when:

  • you want true parallelism (use processes)
  • the logic is simple and synchronous
  • you don’t control the event loop
  • the code becomes harder to reason about
  • you’re tempted to “fiber‑ize” everything

Fibers are a foundation,
not a pattern to sprinkle everywhere
.


7. When AI Gets This Wrong

AI often:

  • treats Fibers like threads
  • mixes Fiber usage with async/await patterns from other languages
  • suspends or resumes at unsafe points
  • uses Fibers where a simple function call is clearer
  • invents event loops that don’t exist

Our mental model helps us see when a Fiber expresses real concurrency
—and when AI is using it simply because it “looks asynchronous”.


Summary

Fibers bring cooperative concurrency to PHP without breaking its single‑threaded nature.
They make asynchronous behavior explicit, predictable, and readable.

Once we internalize this,
we can immediately see when AI‑generated code:

  • misuses Fibers as threads
  • invents async patterns that don’t exist
  • suspends at unsafe boundaries
  • or misses opportunities to simplify async flow

Fibers are a deep feature
— but they teach us
how modern PHP wants to express concurrency with clarity and control.


advanced_concepts/php_fibers.txt · Last modified: by editor