PHP code example of lanzhi / php-coroutine

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

    

lanzhi / php-coroutine example snippets




use lanzhi\coroutine\AbstractRoutine;
use lanzhi\coroutine\Scheduler;

class Routine1 extends AbstractRoutine
{
    protected function generate() : Generator
    {
        yield;
        echo get_called_class() , ": step 1\n";

        yield;
        echo get_called_class(), ": step 2\n";

        yield;
        echo get_called_class(), ": step 3\n";

        return get_called_class() . ": return";
    }
}
class Routine2 extends AbstractRoutine
{
    protected function generate() : Generator
    {
        yield;
        echo get_called_class() , ": step 1\n";

        yield;
        echo get_called_class(), ": step 2\n";

        yield;
        echo get_called_class(), ": step 3\n";

        return get_called_class() . ": return";
    }
}

$scheduler = Scheduler::getInstance();

$scheduler->register(new Routine1());
$scheduler->register(new Routine2());

$scheduler->run();