Promises in PHP

We just released sabre/event 2.0 last week, featuring support for Promises.

It’s still kinda rare to need Promises in PHP. PHP isn’t asynchronous the same way languages such as javascript are, and there’s no real eventloop, unless you’re one of the few that use libevent in PHP. It actually took me a long time to fully grasp promises up to a point where I was comfortable writing an implementation.

So despite being interested in the pattern, it took a while for me to find a use-case where it actually made sense to deploy it.

Recently I was tasked to write a client for a webservice that would require:

  1. Lots and lots of HTTP request for many users.
  2. Some of which were required to happen in a specific order.
  3. Others could happen in parallel.

Using Promises was absolutely ideal here, and a lot of fun. So I added it to the sabre/event library.

I managed to compress the full concept into a single, relatively simple class, but it took a fair bit of step-through debugging to figure out what I did wrong at times.

The full documentation for it can be found on the website. I hope others have some use for it!