current_context

(PHP 8.6+, True Async 1.0)

current_context() — Returns the Async\Context object bound to the current Scope.

Description

current_context(): Async\Context

If the context for the current Scope has not been created yet, it is created automatically. Values set in this context are visible to all coroutines in the current Scope via find().

Return Values

An Async\Context object.

Examples

<?php
use function Async\current_context;
use function Async\spawn;

current_context()->set('request_id', 'abc-123');

spawn(function() {
    // Sees the value from the parent scope
    $id = current_context()->find('request_id'); // "abc-123"
});
?>

See Also