subscribe

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!

Web mentions

Comments

  • Robin Speekenbrink

    Cool and looks great! Have been following the Sabre* developments from afar for a while now. One thing i was wondering: have you looked at the `other` big promise implementation from ReactPhp ? (https://github.com/reactphp...

    Are you intentionally creating a very basic promise interface or are you planning to make it more consise with other (js based) PromiseA+ spec (https://github.com/promises... implementations (like When.js et al)

    • Evert

      Evert

      I'm pretty sure that this lib is largely compliant, with two exceptions that I'm aware of:

      1. Left out the 'thenable' part. This made a lot less to me in PHP
      2. If a promise is already resolved, then onFullfilled/onRejected callbacks are called immediately. This, because we can't schedule a function to be called in a next 'tick' in PHP.

      If I missed something aside from that, I would love to hear it. As for the reactphp implementation... I had too much trouble understanding how it worked. I frankly thought I could do it subjectively 'better'.

      ReactPHP's promise library has callbacks for onProgress, and has a bigger object model, which would allow for deeper customization. I can only assume that this was needed for use-cases that I couldn't think of ;).

      • Robin Speekenbrink

        True. Your approach is alot more readable and compact. I guess i just got used to the whenjs library and the various helper methods (otherwise() / spread(), done() etc)

        All in all, your way of building the promise allows for more easier extension :)

        Thanks for the hard work!

  • Xu Ding

    Thanks for sharing.

    But how did your Promise solve the single threaded PHP issue? From my understanding, you will need to use pcntl_fork to create child process in PHP to make it truly multiple threaded.

    Correct me if I am wrong.

    Regards
    Xu

    • Evert

      Evert

      Hi Xu Ding,

      The type of concurrency this is is similar to nodejs. There's only one process, one thread of PHP being executed at once. However, IO (such as network IO and disk IO) is done on different threads. If you're curious how this would work in PHP, I would suggest you research how it works in node.js. There's likely a lot more written material about that.

      Ever