Since: 8.1

final class Fiber (View source)

Fibers represent full-stack, interruptible functions. Fibers may be suspended from anywhere in the call-stack, pausing execution within the fiber until the fiber is resumed at a later time.

Methods

__construct(callable $callback)

Creates a new Fiber instance

mixed
start(mixed ...$args)

Starts execution of the fiber. Returns when the fiber suspends or terminates.

mixed
resume(mixed $value = null)

Resumes the fiber, returning the given value from {Fiber::suspend()}.

mixed
throw(Throwable $exception)

Throws the given exception into the fiber from {Fiber::suspend()}.

bool
isStarted()

Determines if the fiber has started

bool
isSuspended()

Determines if the fiber is suspended

bool
isRunning()

Determines if the fiber is running

bool
isTerminated()

Determines if the fiber has terminated

mixed
getReturn()

Gets the value returned by the Fiber

static Fiber|null
getCurrent()

Gets the currently executing Fiber instance

static mixed
suspend(mixed $value = null)

Suspend execution of the fiber. The fiber may be resumed with {Fiber::resume()} or {Fiber::throw()}.

Details

__construct(callable $callback)

Creates a new Fiber instance

Parameters

callable $callback

Function to invoke when starting the fiber.

mixed start(mixed ...$args)

Starts execution of the fiber. Returns when the fiber suspends or terminates.

Parameters

mixed ...$args

Arguments passed to fiber function.

Return Value

mixed

Value from the first suspension point or NULL if the fiber returns.

Exceptions

FiberError
Throwable

mixed resume(mixed $value = null)

Resumes the fiber, returning the given value from {Fiber::suspend()}.

Returns when the fiber suspends or terminates.

Parameters

mixed $value

The value to resume the fiber. This value will be the return value of the current Fiber::suspend call.

Return Value

mixed

Value from the next suspension point or NULL if the fiber returns.

Exceptions

FiberError
Throwable

mixed throw(Throwable $exception)

Throws the given exception into the fiber from {Fiber::suspend()}.

Returns when the fiber suspends or terminates.

Parameters

Throwable $exception

The exception to throw into the fiber from the current Fiber::suspend call.

Return Value

mixed

Value from the next suspension point or NULL if the fiber returns.

Exceptions

FiberError
Throwable

bool isStarted()

Determines if the fiber has started

Return Value

bool

True if the fiber has been started.

bool isSuspended()

Determines if the fiber is suspended

Return Value

bool

True if the fiber is suspended.

bool isRunning()

Determines if the fiber is running

Return Value

bool

True if the fiber is currently running.

bool isTerminated()

Determines if the fiber has terminated

Return Value

bool

True if the fiber has completed execution (returned or threw).

mixed getReturn()

Gets the value returned by the Fiber

Return Value

mixed

Return value of the fiber callback. NULL is returned if the fiber does not have a return statement.

Exceptions

FiberError

static Fiber|null getCurrent()

Gets the currently executing Fiber instance

Return Value

Fiber|null

Returns the currently executing fiber instance or NULL if in {main}.

static mixed suspend(mixed $value = null)

Suspend execution of the fiber. The fiber may be resumed with {Fiber::resume()} or {Fiber::throw()}.

Cannot be called from {main}.

Parameters

mixed $value

Value to return from {\Fiber::resume()} or {\Fiber::throw()}.

Return Value

mixed

Value provided to {\Fiber::resume()}.

Exceptions

FiberError
Throwable