In this article, we will look at the new Fibers Feature in the future PHP update

PHP continues to improve features in its codebase. PHP Fibers will appear in PHP 8.1 later this year, and this is one of the important additions to the language. They will open up new possibilities for a kind of asynchronous programming (coroutines). The fiber concept mainly refers to the lightweight flow of execution (also called coroutine). They run in parallel, but they are not sent directly to the CPU and are eventually processed by the runtime itself. Many major languages have their own ways of implementing them, but they have one similarity and it is as follows: let the computer perform two or more tasks simultaneously, and wait until everything is completed.

The implementation of Fibers in PHP is not true asynchronous computing, as someone might think. Obviously, PHP core will still be synchronous after Fibers are introduced. You can imagine PHP Fibers are like switching from one car to another.

How Do Will Fibers Work?

A Fiber is a single final class and is similar to threads in a computer program. Threads are scheduled by the operating system and does not guarantee when and at which point the threads are paused and resumed. Fibers are created, started, suspended, and terminated by the program itself, and allows fine control of the main program execution and the Fiber execution.

final class Fiber
{
    public function __construct(callable $callback) {}

    public function start(mixed ...$args): mixed {}

    public function resume(mixed $value = null): mixed {}

    public function throw(Throwable $exception): mixed {}

    public function isStarted(): bool {}

    public function isSuspended(): bool {}

    public function isRunning(): bool {}

    public function isTerminated(): bool {}

    public function getReturn(): mixed {}

    public static function this(): ?self {}

    public static function suspend(mixed $value = null): mixed {}
}

#php8 #news #fiber #coding

PHP 8.1: New Features You Need To Know About
1.75 GEEK