coroutine_context

(PHP 8.6+, True Async 1.0)

coroutine_context() — Returns the Async\Context object bound to the current coroutine.

Description

php
coroutine_context(): Async\Context

Returns the private context of the current coroutine. Data set here is not visible to other coroutines. If the context for the coroutine has not been created yet, it is created automatically.

Return Values

An Async\Context object.

Examples

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

spawn(function() {
    coroutine_context()->set('step', 1);
    // Later in the same coroutine
    $step = coroutine_context()->getLocal('step'); // 1
});

spawn(function() {
    // Cannot see 'step' from another coroutine
    $step = coroutine_context()->findLocal('step'); // null
});
?>

See Also