Download the PHP package phpgt/promise without Composer
On this page you can find all versions of the php package phpgt/promise. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download phpgt/promise
More information about phpgt/promise
Files in phpgt/promise
Informations about the package promise
A pleasant way to work with asynchronous PHP.
There are many implementations of the concept of a Promise
. This library aims to be compatible with the Web API's Promise implementation, providing a then
, catch
and finally
mechanism that behave the same as when working with promises in the web browser.
In computer science, a Promise
is a mechanism that provides a simple and direct relationship between procedural code and asynchronous callbacks. Functions within procedural languages, like plain old PHP, have two ways they can affect your program's flow: either by returning values or throwing exceptions.
When working with functions that execute asynchronously, we can't return values because they might not be ready yet, and we can't throw exceptions because that's a procedural concept (where should we catch them?). That's where promises come in: instead of returning a value or throwing an exception, your functions can return a Promise
, which is an object that can be fulfilled with a value, or rejected with an exception, but not necessarily at the point that they are returned.
With this concept, the actual work that calculates or loads the value required by your code can be deferred to a task that executes asynchronously. Behind the scenes of PHP.Gt/Promise is a Deferred
class that is used for exactly this.
Example usage
The following is an example of the syntax provided by this library.
Deferred
and Promise
objects
This repository splits the responsibility of the asynchronous task's processing and the result of the task completion into the Deferred
and Promise
classes respectively.
A Deferred
object is assigned one or more "process" callbacks, which will be called in order to execute the deferred task.
A Promise
is created by the Deferred
upon construction, which is used to represent the result of the deferred task's completion.
To make a class work with promises, it needs at least two functions: one public function that constructs the Deferred object and returns the Promise, and one function that is assigned as the Deferred's process function.
See the example code layout below:
The simplest representation of how the above Example class would be integrated is as follows:
The end result is the ability to call the public function, without needing to know anything about Promise/Deferred implementation.
Important: PHP is still a fully procedural language, so without an external loop being used to call the deferred process, the promise will never fulfil or reject.
Event loop
In order for this Promise library to be useful, some code has got to act as an event loop to call the deferred processes. This could be a simple while loop, but for real world tasks a more comprehensive loop system should be used.
The implementation of a Promise-based architecture is complex enough on its own, so the responsibility of an event loop library is maintained separately in PHP.Gt/Async.
Special thanks
The work put into the development of this repository is mainly thanks to the great work and inspiration given by reactphp's promise implementation and the superb writing of Domenic Denicola.