Content · Programming · PHP

PHP Fibers and Cooperative Concurrency

PHP Fibers and Cooperative Concurrency: Several Fibers can cooperatively yield control to a scheduler without creating operating-system threads. See three examples and important limitations.

1 views Published Oct 17, 2026 Updated Oct 17, 2026 Know-Stuff

Several Fibers can cooperatively yield control to a scheduler without creating operating-system threads.

The examples below begin with the core behavior, then move to an application-oriented use.

How PHP Fibers and Cooperative Concurrency Works


Several Fibers can cooperatively yield control to a scheduler without creating operating-system threads.

Example 1: Basic Usage


This example uses a small input to make the main behavior visible.

<?php

$fiber = new Fiber(function (): void { echo "step 1 "; Fiber::suspend(); echo "step 2"; });
$fiber->start();
$fiber->resume();

Output:

step 1 step 2

Several Fibers can cooperatively yield control to a scheduler without creating operating-system threads.

Example 2: A Different Input


A different set of values shows the same operation in another situation.

<?php

$fibers = [new Fiber(fn() => Fiber::suspend("A")), new Fiber(fn() => Fiber::suspend("B"))];
foreach ($fibers as $fiber) { echo $fiber->start(); }
foreach ($fibers as $fiber) { $fiber->resume(); }

Output:

AB

Fibers do not create parallel CPU execution and require explicit scheduling or an event loop.

Practical Example: Applying the Feature


This example shows an application-style use of the feature, with its surrounding setup visible.

<?php

$firstFiber = new Fiber(function (): string { Fiber::suspend("first paused"); return "first done"; });
$secondFiber = new Fiber(function (): string { Fiber::suspend("second paused"); return "second done"; });
$firstFiber->start();
$secondFiber->start();
$firstFiber->resume();
$secondFiber->resume();
echo $firstFiber->getReturn();

Output:

first done

The code is shown for learning. Adjust the class names, inputs and error handling to suit your application.

Important Behavior and Edge Cases


  • Fibers do not create parallel CPU execution and require explicit scheduling or an event loop.
  • Several Fibers can cooperatively yield control to a scheduler without creating operating-system threads.
  • Observe differences in empty inputs, invalid states and optional extension availability where relevant.

Comparison With Related Approaches


ApproachImportant Difference
PHP Fibers and Cooperative ConcurrencySeveral Fibers can cooperatively yield control to a scheduler without creating operating-system threads.
Alternative approachPromises and event loops may provide higher-level scheduling on top of fibers.

Common Beginner Mistakes


MistakeCorrect Approach
Ignoring the documented state or precondition of PHP Fibers and Cooperative ConcurrencyFibers do not create parallel CPU execution and require explicit scheduling or an event loop.
Expecting a different type of resultRead the declared return type and distinguish returned values from changes to the source object.
Assuming a related API works identicallyPromises and event loops may provide higher-level scheduling on top of fibers.

Key Takeaways


  • Several Fibers can cooperatively yield control to a scheduler without creating operating-system threads.
  • Fibers do not create parallel CPU execution and require explicit scheduling or an event loop.
  • Use the example that matches the values, runtime version and dependencies in your application.

PHP Fibers and Cooperative Concurrency is useful when its behavior matches the data structure and lifecycle of your PHP application. Choose it with a clear understanding of its preconditions and returned values.

Share this resourceSend this page to someone who may find it useful.