PHP Classes

Simple PHP Promise Library: Register functions to call when conditions are met

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 128 All time: 9,385 This week: 660Up
Version License PHP version Categories
simple-promise 1.0The PHP License5PHP 5, Language
Description 

Author

This package can register functions to call when conditions are met.

It provides a promise class that can register one callback function to be called when a condition is true. It can also call another callback function to be called when the condition is false.

The package also provides a deferred class to register a new promise to evaluate the conditions again later.

Innovation Award
PHP Programming Innovation award nominee
August 2020
Number 3
A promise is a control structure that you can use in your code to define the actions to be executed when a condition is true, false, or needs to be evaluate later again while the condition status is not yet determined.

This package provides means to implement promises in PHP by evaluating conditions now or later using deferred promises.

Manuel Lemos
Picture of Ahmad Mustapha
Name: Ahmad Mustapha <contact>
Classes: 24 packages by
Country: Nigeria Nigeria
Age: ???
All time rank: 226511 in Nigeria Nigeria
Week rank: 48 Up2 in Nigeria Nigeria Up
Innovation award
Innovation award
Nominee: 9x

Example

<?php
use SimplePromise\Deferred;

require
'vendor/autoload.php';

function
test($number)
{
   
$deferred = new Deferred();

    if (
$number > 2){
       
$deferred->resolve('Succeeded');
    }else{
       
$deferred->reject('Failed');
    }

    return
$deferred->promise();
}

test(1)->then(function ($data){
    echo
"First: {$data} \n";
})->
otherwise(function ($error){
    echo
"First: {$error} \n";
});

test(4)->then(function ($data){
    echo
"Second: {$data} \n";
})->
otherwise(function ($error){
    echo
"Second: {$error} \n";
});


Details

Simple Promise

A simple PHP promise library that works synchronously.

Note

Please note that this library cannot be used in Asynchronous projects, projects like ReactPHP or Amphp.

Installation

Make sure that you have composer installed Composer.

If you don't have Composer run the below command

curl -sS https://getcomposer.org/installer | php

Run the installation

composer require ahmard/simple-promise ^1.0

Usage

<?php
use SimplePromise\Deferred;

require 'vendor/autoload.php';

function test($number)
{
    $deferred = new Deferred();
    
    if ($number > 2){
        $deferred->resolve('Succeeded');
    }else{
        $deferred->reject('Failed');
    }
    
    return $deferred->promise();
}

test(1)->then(function ($data){
    echo $data;
})->otherwise(function ($error){
    echo $error;
});

Examples


  Files folder image Files (11)  
File Role Description
Files folder image.idea (5 files)
Files folder imageexamples (2 files)
Files folder imagesrc (2 files)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file README.md Doc. Read me

  Files folder image Files (11)  /  .idea  
File Role Description
  Accessible without login Plain text file misc.xml Data Auxiliary data
  Accessible without login Plain text file modules.xml Data Auxiliary data
  Accessible without login Plain text file php.xml Data Auxiliary data
  Accessible without login Plain text file simple-promise.iml Data Auxiliary data
  Accessible without login Plain text file vcs.xml Data Auxiliary data

  Files folder image Files (11)  /  examples  
File Role Description
  Accessible without login Plain text file 1-simple.php Example Example script
  Accessible without login Plain text file 2-exception.php Example Example script

  Files folder image Files (11)  /  src  
File Role Description
  Plain text file Deferred.php Class Class source
  Plain text file Promise.php Class Class source

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:128
This week:0
All time:9,385
This week:660Up