ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
## Coroutine::getContext 获取当前协程的上下文对象 ~~~ function Coroutine::getContext() : Coroutine\Context ~~~ 接受一个可选参数cid, 默认返回当前协程的上下文对象 <br> ~~~ function func(callable $fn, ...$args) { go(function () use ($fn, $args) { $fn(...$args); echo 'Coroutine#' . Co::getCid() . ' exit' . PHP_EOL; }); } /** * Compatibility for lower version * @param object|Resource $object * @return int */ 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; } } $context = new Co\Context(); assert($context instanceof ArrayObject); assert(Co::getContext() === null); func(function () { $context = Co::getContext(); assert($context instanceof Co\Context); $context['resource1'] = new Resource; $context->resource2 = new Resource; func(function () { Co::getContext()['resource3'] = new Resource; Co::yield(); Co::getContext()['resource3']->resource4 = new Resource; Co::getContext()->resource5 = new Resource; }); }); Co::resume(2); ~~~