0Pricing
PHP Academy · Lesson

Fibers: Cooperative Concurrency

Pause and resume PHP execution with Fibers for async-like patterns.

Fibers: Cooperative Concurrency is a free PHP Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the PHP Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What Are Fibers?

PHP 8.1 introduced Fibers — lightweight coroutines that can pause execution at a specific point and be resumed later, enabling cooperative multitasking.

Fiber vs Async

Fibers are not parallel threads — PHP is still single-threaded. Fibers allow one execution context to voluntarily yield control to another, enabling async-like patterns.

Creating a Fiber

Create a Fiber by passing a callable to its constructor. The fiber does not start until you call start().

<?php
$fiber = new Fiber(function (): void {
    echo "Fiber started\n";
    $value = Fiber::suspend("first suspension");
    echo "Resumed with: $value\n";
});

Starting and Suspending

start() runs the fiber until the first Fiber::suspend(). The value passed to suspend() is returned from start().

<?php
$suspended = $fiber->start(); // runs until suspend()
echo $suspended; // "first suspension"

Resuming a Fiber

resume($value) continues the fiber from where it suspended. The value passed to resume() is returned by Fiber::suspend() inside the fiber.

<?php
$fiber->resume("hello from main");
// Inside fiber: $value = "hello from main"
// Prints: "Resumed with: hello from main"

Complete Fiber Example

A full fiber lifecycle: create, start, suspend, resume, terminate.

<?php
$fiber = new Fiber(function (): string {
    $x = Fiber::suspend("paused");
    return "Fiber done, received: $x";
});

$msg = $fiber->start();   // "paused"
$fiber->resume("hi!");
echo $fiber->getReturn(); // "Fiber done, received: hi!"

Fiber Status Methods

Check fiber state: isStarted(), isSuspended(), isRunning(), isTerminated().

<?php
var_dump($fiber->isTerminated()); // true after getReturn()

Error Handling in Fibers

Throw an exception into a fiber with $fiber->throw(new Exception(...)). The exception appears at the Fiber::suspend() point inside the fiber.

Event Loop Integration

Fibers are designed to power event loops (ReactPHP, Revolt) where I/O callbacks suspend while waiting and resume when data arrives.

Fibers vs Generators

Generators (PHP 5.5+) also pause with yield, but are primarily for lazy iteration. Fibers are a more general coroutine mechanism with bidirectional value passing and return values.

Practical Use Cases

Fibers power: non-blocking HTTP clients in Revolt, async database queries, cooperative task schedulers, and streaming data pipelines without multi-threading complexity.

Summary

Fibers allow one PHP execution context to pause and resume, enabling cooperative concurrency. They underpin modern PHP async frameworks. Fiber::suspend() yields control; resume() restores it.

Quick Check

How do you pass a value into a suspended Fiber?

Frequently asked questions

Is the “Fibers: Cooperative Concurrency” lesson free?

Yes — the full text of “Fibers: Cooperative Concurrency” is free to read here on the web, and the PHP Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the PHP Academy course, upgrade to CoddyKit PRO.

What will I learn in “Fibers: Cooperative Concurrency”?

Pause and resume PHP execution with Fibers for async-like patterns. You practise PHP Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start PHP Academy?

No prior experience is required. PHP Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Fibers: Cooperative Concurrency” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this PHP Academy lesson?

Yes. Every PHP Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Match Expression and Named Arguments
  2. Nullsafe Operator and Union Types
  3. Enumerations (Enums)
  4. Fibers: Cooperative Concurrency
← Back to PHP Academy