PHP code example of arkanmgerges / worker

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

    

arkanmgerges / worker example snippets


Use Worker\Worker

$worker = new Worker(
    // Here you can provide your main callback
    function($arg1 = '', $arg2 = '') {
        file_put_contents('result.txt', $arg1 . $arg2);
    },
    // The second one is used when main callback has completed successfully
    function() {
        file_put_contents('success.txt', 'success');
    },
    // If an exception has happened in the main callback then this callback will be called with an error message
    function($e) {
        file_put_contents('error.txt', 'error');
    }
);

// Start the worker, and pass 2 arguments to the main callback. It is also possible to pass more arguments
$worker->start('first arg', 'second arg');

class SomeClass
{
    public function method($arg1, $arg2, $arg3)
    {
        file_put_contents('result.txt', $arg1 . $arg2 . $arg3);
    }
};

$object = new SomeClass();
// Pass array, first item is the object and second item is the name of the class method
$worker = new Worker([$object, 'method']);
// Start worker and send 3 arguments
$worker->start('from', ' object method', ', this is nice');

class SomeClass
{
    public static function method($arg1, $arg2, $arg3)
    {
        file_put_contents('result.txt', $arg1 . $arg2 . $arg3);
    }
};

$worker = new Worker(__NAMESPACE__ . '\SomeClass::method');
$worker->start('from', ' class method', ', nice');
sh
php composer.phar update