May 4, 2024
Breaking News

PHP 8.1: New Features You Need To Know About – hackernoon.com

npressfetimg-7851.png

PHP Fibers will appear in PHP 8.1 later this year. Fibers are a lightweight flow of execution (also called coroutines) Fibers run in parallel, but they are not sent directly to the CPU. They are similar to threads in a computer program, but are not scheduled by the operating system. PHP core will still be synchronous after Fibers introduced, but that doesn’t mean your application can’t do two things at the same time. Fibers allow fine control of the main program execution and the Fiber execution.

Oleg Melnic

Technical Lead in the company Proxify

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 </.......

Source: https://hackernoon.com/php-81-new-features-you-need-to-know-about-snz33bk