update page now

Fiber::getCurrent

(PHP 8 >= 8.1.0)

Fiber::getCurrentGets the currently executing Fiber instance

Description

public static Fiber::getCurrent(): ?Fiber

Parameters

This function has no parameters.

Return Values

Returns the currently executing Fiber instance or null if this method is called from outside a fiber.

add a note

User Contributed Notes 1 note

up
0
booleantype1990 at gmail dot com
6 months ago
Fiber::getCurrent() will only return true if you call it from within a Fiber while it is running.

<?php
$fiber = new Fiber(function () {
    echo "Inside Fiber. getCurrent: ";
    var_dump(Fiber::getCurrent()); // Here is object(Fiber)#1 (0) { }

    Fiber::suspend();

    echo "Inside Fiber again after resume(). getCurrent: ";
    var_dump(Fiber::getCurrent()); // object(Fiber)#1 (0) { } again
});

echo "Before Fiber launch. getCurrent: ";
var_dump($fiber->getCurrent()); // Here is NULL 

$fiber->start();

echo "After suspend. getCurrent: ";
var_dump($fiber->getCurrent()); // Here is NULL 

$fiber->resume();

echo "After resume. getCurrent: ";
var_dump($fiber->getCurrent()); // Here is NULL 
?>

Result:
Before Fiber launch. getCurrent: NULL
Inside Fiber. getCurrent: object(Fiber)#1 (0) {}
After suspend. getCurrent: NULL
Inside Fiber again after resume(). getCurrent: object(Fiber)#1 (0) {}
After resume. getCurrent: NULL
To Top