展示相同功能的示例,说明Fiber和Generator之间的区别
<?php
$gener = (function () use (&$gener): Generator {
$userfunc = function () use (&$gener) : Generator {
register_shutdown_function(function () use (&$gener) {
$gener->send('test');
});
return yield 'test';
};
$parm = yield from $userfunc();
echo "用于恢复协程的值: ", $parm, PHP_EOL;
})();
$res = $gener->current();
echo "协程挂起时的值: ", $res, PHP_EOL;
?>
<?php
$fiber = new Fiber(function () use (&$fiber) : void {
$userfunc = function () use (&$fiber) : string {
register_shutdown_function(function () use (&$fiber) {
$fiber->resume('test');
});
return Fiber::suspend('fiber');
};
$parm = $userfunc();
echo "用于恢复协程的值: ", $parm, PHP_EOL;
});
$res = $fiber->start();
echo "协程挂起时的值: ", $res, PHP_EOL;
?>