Coroutine::getContext

(PHP 8.6+, True Async 1.0)

public Coroutine::getContext(): Async\Context

Returns the local context of the coroutine. The context is created lazily on first access.

The context allows storing data bound to a specific coroutine and passing it to child coroutines.

Return Value

Async\Context – the coroutine’s context object.

Examples

Example #1 Accessing the context

<?php

use function Async\spawn;
use function Async\await;

$coroutine = spawn(function() {
    $ctx = \Async\current_context();
    $ctx['request_id'] = uniqid();

    return $ctx['request_id'];
});

await($coroutine);
$ctx = $coroutine->getContext();

See Also