Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / PHP

Fibers and cooperative scheduling

A fiber can suspend from a helper several calls deep, preserving that stack until resumed.

Version: PHP 8.4; repository runtime, production runtime not attested

Syntax

Fiber::suspend($request); $fiber->resume($response);

A fiber can suspend from a helper several calls deep, preserving that stack until resumed. The value passed to suspend goes to the caller; the value given to resume becomes the result of suspend inside the fiber. On normal termination resume returns null and getReturn exposes the final result. Resuming a terminated fiber raises FiberError.

function request(){return Fiber::suspend("ticket");}$f=new Fiber(fn()=>request()+1);$out=$f->start();$last=$f->resume(6);try{$f->resume();}catch(FiberError $e){$failure=get_class($e);}echo json_encode([$out,$last,$f->getReturn(),$failure]);
["ticket",null,7,"FiberError"]

Examples

<?php
function request(){return Fiber::suspend("ticket");}$f=new Fiber(fn()=>request()+1);$out=$f->start();$last=$f->resume(6);try{$f->resume();}catch(FiberError $e){$failure=get_class($e);}echo json_encode([$out,$last,$f->getReturn(),$failure]);

Result

["ticket",null,7,"FiberError"]

Sources