OpenSwoole\Coroutine::getContext(int $cid = 0)

Latest version: pecl install openswoole-22.1.2 | composer require openswoole/core:22.1.5

Declaration

<?php OpenSwoole\Coroutine::getContext(int $cid = 0)

Parameters

cid

Coroutine ID. By default, use the ID of current coroutine.

Return

context

The context of a coroutine

Description

Get the context of a coroutine by coroutine ID.

Example

<?php
function php_object_id($object)
{
    static $id = 0;
    static $map = [];
    $hash = spl_object_hash($object);
    return $map[$hash] ?? ($map[$hash] = ++$id);
}

class Resource
{
    public function __construct()
    {
        echo __CLASS__ . '#' . php_object_id((object)$this) . ' constructed' . PHP_EOL;
    }

    public function __destruct()
    {
        echo __CLASS__ . '#' . php_object_id((object)$this) . ' destructed' . PHP_EOL;
    }
}

co::run(function () {
    $context = OpenSwoole\Coroutine::getContext();
    $context['resource1'] = new Resource;
    var_dump($context);
});
Last updated on September 1, 2022