Fibers in PHP: A new opportunity for async PHP?

Click for: original source

It looks like PHP will get fibers soon with PHP 8.1! That’s awesome! Or is it. By Christian Lück.

Fibers are primitives for implementing light weight cooperative concurrency [in Ruby]. Basically they are a means of creating code blocks that can be paused and resumed, much like threads. The main difference is that they are never preempted and that the scheduling must be done by the programmer and not the VM.

The article then covers:

  • What are fibers?
  • Will fibers bring async to PHP?
  • Do we need fibers for async PHP?
  • What problem do fibers solve?

As detailed in the previous section, we need a scheduler (or event loop) in order to run things asynchronously or concurrently. This means you would still have to use something like ReactPHP, Swoole or Amp for async PHP. With or without fibers, async PHP will be provided by external libraries. Good read!

In fact, with fibers you will no longer see that a function call is asynchronous at all. Fibers allow you to express a synchronous program flow, so you don’t have to deal with any async execution at all. Interestingly, this also means the average PHP application developer will also never interface with the Fiber implementation at all. I think this is a great plus. Good read!

[Read More]

Tags web-development app-development php functional-programming