PHP code example of jgswift / bubblr

1. Go to this page and download the library: Download jgswift/bubblr 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/ */

    

jgswift / bubblr example snippets


interface BubbleInterface extends EventInterface, EventAwareInterface {

    public function getResult();

    public function setContext($context);

    public function resume(SpoutInterface $spout);

    public function suspend(SpoutInterface $spout);
}

interface SpoutInterface extends BubblerAwareInterface {

    public function push(BubbleInterface $bubble);

    public function pop();

    public function invoke(BubbleInterface $bubble);

    public function suspend();

    public function resume();
}

interface BubblerInterface {

    public function execute($bubble = null);

    public function attach(SpoutInterface $spout);

    public function detach(SpoutInterface $spout);

    public function resume();

    public function suspend();

    public function getSpout();
}

$hello = bubblr\async(function() {
    return 'hello world';
});

echo $hello(); // 'hello world'

function hello() {
    return 'hello world';
}

$hello = bubblr\async('hello');

echo $hello(); // 'hello world'

$fib = bubblr\async(function($num) {
    $n1 = 0; $n2 = 1; $n3 = 1;
    for ($i = 2; $i < $num; ++$i) {
        $n3 = $n1 + $n2;
        $n1 = $n2;
        $n2 = $n3;
        // every 50 iterations this bubble will suspend and allow other waiting bubbles to execute
        if (!($i % 50)) { 
              bubblr\reschedule();
        }
    }
    return $n3;
});

echo is_infinite($fib(3000)); // true

function hello() {
    return 'hello';
}

function world() {
    return 'world';
}

$results = bubblr\asyncAll([
    'hello',
    'world'
]);

var_dump($results); // Array ['hello','world']

$addition = bubblr\async(function($a,$b) {
    if(!is_numeric($a) || !is_numeric($b)) {
        throw new \InvalidArgumentException();
    }

    return $a + $b;
});

try {
    $addition('bar',5);
} catch(\InvalidArgumentException $e) {
    echo 'Invalid argument';
}
sh
php composer.phar