Join 4,000+ others and never miss out on new tips, tutorials, and more.
Latest version:
pecl install openswoole-22.1.2 | composer require openswoole/core:22.1.5
<?php OpenSwoole\Coroutine::getContext(int $cid = 0)
Coroutine ID. By default, use the ID of current coroutine.
The context of a coroutine
Get the context of a coroutine by coroutine ID.
<?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);
});