PHP code example of n-singularity / async

1. Go to this page and download the library: Download n-singularity/async library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

n-singularity / async example snippets


function sendEmail($email, $bodyEmail){
    //code to send email
}

function foo(){
    Nsingularity\Async\Async::globalFunction("sendEmail", ["[email protected]", "body email html"]);
    echo("done");
}


class Example
{
    public function sendEmail($email, $bodyEmail){
        //code to send email
        
    }
}

function foo(){
    Nsingularity\Async\Async::objectFunction(new Example, "sendEmail", ["[email protected]", "body email html"]);
    echo("done");
}

class Example implements \Nsingularity\Async\AsyncableClassInterface
{
    private $email;
    
    private $bodyEmail;
    
    public function __construct($email, $bodyEmail){
        $this->email = $email;
        $this->bodyEmail = $bodyEmail;
    }
    
    public function handler(){
        //code to send email
        
    }
}

function foo(){
    Nsingularity\Async\Async::object(new Example("[email protected]", "body email html"));
    echo("done");
}

function createText($a, $b)
{
    sleep(5);
    return $a . " " . $b;
}

function foo(){
    $ts = time();
    $handler1 = Nsingularity\Async\Async::globalFunction("createText" ,["hello","world"]);
    $handler2 = Nsingularity\Async\Async::globalFunction("createText" ,["hello","hello"]);
    $handler3 = Nsingularity\Async\Async::globalFunction("createText" ,["world","world"]);
    
    $text1 = $handler1->get();
    $text2 = $handler2->get();
    $text3 = $handler3->get();
    
    $te = time();
    
    echo $text1." | ".$text2." | ".$text3." | ".$te-$ts; // hello world | hello hello | world world | 6
}