Content · Programming · PHP

PHP Fiber::getCurrent() Method: Usage and Examples

PHP Fiber::getCurrent() Method: Usage and Examples: Returns the Fiber currently running in this execution context, or null outside a fiber. See three examples and important limitations.

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

The PHP Fiber::getCurrent API performs a focused operation on an object or value. The examples below show its effect on actual data.

Returns the Fiber currently running in this execution context, or null outside a fiber.

What Is Fiber::getCurrent in PHP?


Returns the Fiber currently running in this execution context, or null outside a fiber.

Syntax:

Fiber::getCurrent(): ?Fiber

This operation has no parameters.

Return type declared by PHP: ?Fiber. The value or side effect is demonstrated in the examples below.

Example 1: Basic Usage


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

<?php

$fiber = new Fiber(function (): string {
    $input = Fiber::suspend("waiting");
    return "completed: " . $input;
});
$inside = new Fiber(function (): void { var_dump(Fiber::getCurrent() instanceof Fiber); });
$inside->start();

Output:

bool(true)

Returns the Fiber currently running in this execution context, or null outside a fiber.

Example 2: A Different Input


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

<?php

$fiber = new Fiber(function (): string {
    $input = Fiber::suspend("request");
    return "completed: " . $input;
});
$inside = new Fiber(function (): void { var_dump(Fiber::getCurrent() !== null); });
$inside->start();

Output:

bool(true)

Outside a Fiber or Closure, a similarly named method has a different contract or availability.

Practical Example: Applying the Feature


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

<?php

$fiber = new Fiber(function (): string {
    $input = Fiber::suspend("pending");
    return "completed: " . $input;
});
$inside = new Fiber(function (): void { var_dump(Fiber::getCurrent() === Fiber::getCurrent()); });
$inside->start();

Output:

bool(true)

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

Important Behavior and Edge Cases


  • Outside a Fiber or Closure, a similarly named method has a different contract or availability.
  • Returns the Fiber currently running in this execution context, or null outside a fiber.
  • Observe differences in empty inputs, invalid states and optional extension availability where relevant.

Comparison With Related Approaches


ApproachImportant Difference
Fiber::getCurrentReturns the Fiber currently running in this execution context, or null outside a fiber.
GeneratorA Fiber suspends a whole call stack and supports resume() and throw(), while a Generator primarily yields sequence values.

Common Beginner Mistakes


MistakeCorrect Approach
Ignoring the documented state or precondition of Fiber::getCurrentOutside a Fiber or Closure, a similarly named method has a different contract or availability.
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 identicallyA Fiber suspends a whole call stack and supports resume() and throw(), while a Generator primarily yields sequence values.

Key Takeaways


  • Returns the Fiber currently running in this execution context, or null outside a fiber.
  • Outside a Fiber or Closure, a similarly named method has a different contract or availability.
  • Use the example that matches the values, runtime version and dependencies in your application.

Fiber::getCurrent 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.