Coroutine::getContext

(PHP 8.6+, True Async 1.0)

public Coroutine::getContext(): Async\Context

Gibt den lokalen Kontext der Coroutine zurueck. Der Kontext wird beim ersten Zugriff lazy erstellt.

Der Kontext ermoeglicht das Speichern von Daten, die an eine bestimmte Coroutine gebunden sind, und deren Weitergabe an Kind-Coroutinen.

Rueckgabewert

Async\Context – das Kontextobjekt der Coroutine.

Beispiele

Beispiel #1 Zugriff auf den Kontext

<?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();

Siehe auch