Php 8.1 New Features — Fibers

Backend Developer
4 min readJan 12, 2022

--

If you are a PHP dev you know that php has a new version since 25 November 2021. This is my 2nd article that subject these changes in php language. There are lots of new things in PHP and in this article i will explain what are fibers and why we should use them in our projects. And also what kinda pros we will get with using them.

What are the Fibers?

As it is known, PHP tries not to lag behind the times by updating the deficiencies in its core code base. One of precious change is Fibers.Lets explain what are the fibers..
Fibers are mechanisms that facilitate simultaneous execution. They make it easy to implement resumable code blocks that can be suspended from anywhere in the stack.

Fibers API is relatively low level. End-user developers are not expected to interact with it on a regular basis. Instead, Fibers will be integrated into libraries that offer asynchronous APIs and event loop implementations. It is a control flow system that provides simplified high-level abstractions.

Fibers provide a route to asynchronous function calls similar to normal synchronous operations. You can break the common patterns of promises and callbacks. Fibers provides an API for blocking and unblocking implementations of operations by managing suspending and resuming code at appropriate points.

Umm… Can you tell us how Fibers work?

Let’s go deeper and see how they are working and how we can use them. A Fiber is basically a final class that has some method definitions like start, suspended , resume etc.. When you create a fiber and init with a callable function Fiber will not do anything till you started it. Lets see some code to explain better..

As you can see our final class Fiber has a constructor method which gets a callable function.

<?php$fiber = new Fiber(function($x):void{
echo 'Hello World I am a fiber callback';
});

The fiber variable init a Fiber and pass a callback function above but our Fiber still does not doing anything. What the hell? What should we do run this? As you can see we defined some methods in our fiber, start,resume… To run a fiber we should use start method.

<?php
$fiber->start(); // isn't it awesome :)

The start method will run the fiber and its callable function we passed in constructor. As i mentioned above Fibers are async but the example above is not an async example right? Yes its not. We can start a fiber but also we can suspend that till we run again or we can wait for sometime to somethings are ready. More code will make the explanation easier i hope

<?php$fiber = new Fiber(function($x){$fibere->suspend();
echo 'Hello World I am a fiber callback';
});
$fiber->start(); // Why its not running?

As you can see we can suspend the fiber in callback function and it will not work even we start the fiber. So how we will run it? Let’s add resume method to run our fiber.

<?php$fiber = new Fiber(function($x){$fibere->suspend();
echo 'Hello World I am a fiber callback';
});
$fiber->start(); // Why its not running?// Do your other works and call fiber$fiber->resume(); // Aha.. its working

This is not totally an async way but we can still do more things at the same time. Such as init sendMail Fiber with suspended state and register user to db and resume the fiber to send register email.Those 2 action will not block each others.

One of the neat things about start(), suspend(, and resume() is that they accept arguments:

  • The start() method will pass the arguments to the callable and will return whatever the suspend() method receives.
  • The suspend() method returns whatever value the resume() method received.
  • The resume() method returns whatever the next call to suspend() received.

This makes communication between the Main thread and the Fiber relatively easy:

  • resume() is used to put values into the Fiber that are received with suspend() , and
  • suspend() is used to push values out that are received by resume().

This makes the official example way easier to understand:

If you execute the above code, you will receive something like this:

Value from fiber suspending state  will be  => fiber
Value used to resume state will be => test

Fiber makes us closer to have our own web server soon and we will not use apache or nginx no more of course we have time for this but this is a huge step forward going to this state. Hope you had fun reading.See you soon with next article.

--

--