← Back to PHP Course | Chapter 14: Advanced PHP | Lesson 16 of 24

PHP Fibers

A fiber is like a bookmark that lets a task pause in the middle and be resumed later. It allows a program to switch between jobs politely, taking turns.
Syntax
php
$fiber = new Fiber(function () {
    $value = Fiber::suspend(data);   // pause here
});

$fiber->start();
$fiber->resume(value);

What are Fibers?

Fibers, added in PHP 8.1, are lightweight cooperative coroutines that let a function pause partway through and later resume exactly where it left off, useful for building async-style code without threads.

Example: What are Fibers?

php
<?php
// PHP 8.1+
$fiber = new Fiber(function () {
    echo "Fiber started\n";
});
echo get_class($fiber);
?>

Creating and Starting a Fiber

Creating a Fiber wraps a block of code in a closure passed to new Fiber(); nothing runs until you explicitly call start(), which begins executing that closure until it suspends or finishes.

Example: Creating and Starting a Fiber

php
<?php
$fiber = new Fiber(function () {
    // Print "Running inside the fiber\n" to the output
    echo "Running inside the fiber\n";
});
$fiber->start();
?>

Suspending Execution

Calling the static Fiber::suspend() from inside the running closure pauses execution at that exact point and hands control back to whatever code started or resumed the fiber.

Example: Suspending Execution

php
<?php
$fiber = new Fiber(function () {
    // Print "Before suspend\n" to the output
    echo "Before suspend\n";
    Fiber::suspend();
    // Print "After resume\n" to the output
    echo "After resume\n";
});
$fiber->start();
// Print "Back in main code\n" to the output
echo "Back in main code\n";
?>

Resuming Execution

resume() picks a suspended fiber back up exactly where suspend() paused it, optionally passing a value back into the paused code -- this back-and-forth is what enables cooperative scheduling.

Example: Resuming Execution

php
<?php
$fiber = new Fiber(function () {
    // Declare `$value`, set to `Fiber::suspend('paused')`
    $value = Fiber::suspend('paused');
    // Print "Resumed with: $value\n" to the output
    echo "Resumed with: $value\n";
});
$fiber->start();
$fiber->resume('hello');
?>

Checking Fiber States

isStarted(), isSuspended(), and isTerminated() let you check a fiber's current state before acting on it, preventing errors like trying to resume a fiber that has already finished.

Example: Checking Fiber States

php
<?php
$fiber = new Fiber(function () {
    Fiber::suspend();
});
// Print a detailed dump (with types) of `$fiber->isStarted()`
var_dump($fiber->isStarted());
$fiber->start();
// Print a detailed dump (with types) of `$fiber->isSuspended()`
var_dump($fiber->isSuspended());
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Forgetting to call Fiber::resume(), so the suspended fiber never continues.
  2. Calling Fiber::suspend() outside a running fiber, which throws an error.
  3. Treating fibers as threads, when they do not run in parallel.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.