PHP code example of marcopetersen / php-chain

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

    

marcopetersen / php-chain example snippets




use MarcoPetersen\Chain\Chain;
use MarcoPetersen\Chain\Link;

// First we define the links that will be part of our chain...
class AddOne extends Link
{
    public function execute($number)
    {
        return $this->next($number + 1);
    }
}

class EndChain extends Link
{
    public function execute($payload)
    {
        return $payload;
    }
}

// ...after which we chain them up together.
$chain = (new Chain())
    ->then(new AddOne()) // you can pass in instances...
    ->then(AddOne::class) // ...or just the FQCN, if you prefer.
    ->then(EndChain::class) // To end the chain, just don't call `next`.
    ->then(AddOne::class) // This won't get called.

$chain->execute(1); // 3

composer