PHP code example of gwangjinkim / php-multidispatch

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

    

gwangjinkim / php-multidispatch example snippets


$fn = multidispatch();
$fn->setDispatchPolicy(\GwangJinKim\Multidispatch\DispatchPolicy::FirstWins);
// ...or...
$fn->setDispatchPolicy(\GwangJinKim\Multidispatch\DispatchPolicy::LastWins);


use function GwangJinKim\Multidispatch\multidispatch;

interface IA {}
interface IB {}
class CA implements IA, IB {}
class CB implements IA, IB {}

$fn = multidispatch();

// Register different handlers for types
$fn[['IA']] = fn($a) => "This is IA";
$fn[['IB']] = fn($a) => "This is IB";
$fn[['*']]  = fn($a) => "Default!";

// Usage: dispatch based on type
echo $fn(new CA()); // Depending on registration/interface, returns "This is IA" or "This is IB"
echo $fn(new CB()); // Ditto

// Also works for built-in types:
$scalarDispatch = multidispatch();
$scalarDispatch[['int', 'string']] = fn($x, $y) => "Int: $x, String: $y";
echo $scalarDispatch(1, "hello"); // Int: 1, String: hello

// You can always register a general handler:
$fn[['*']] = fn($a) => "Fallback/default handler";
echo $fn([]); // Fallback/default handler

$fn = multidispatch();
$fn->setDispatchPolicy(\GwangJinKim\Multidispatch\DispatchPolicy::FirstWins);
// ...or...
$fn->setDispatchPolicy(\GwangJinKim\Multidispatch\DispatchPolicy::LastWins);

$fn[ [Type1, ...], ':before' ] = function ($a, ...) { /* ... */ };
$fn[ [Type1, ...], ':primary' ] = function ($a, ...) { /* ... */ };
$fn[ [Type1, ...], ':after'  ] = function ($a, ...) { /* ... */ };
$fn[ [Type1, ...], ':around' ] = function ($callNext, $a, ...) { /* ... */ };


use function GwangJinKim\Multidispatch\multidispatch;

interface IA {}
class A implements IA {}
class B extends A {}

$fn = multidispatch();

$fn[ [A::class], ':primary' ] = fn($a) => "Primary A";
$fn[ [A::class], ':before'  ] = fn($a) => print("Before A\n");
$fn[ [A::class], ':after'   ] = fn($a) => print("After A\n");
$fn[ [A::class], ':around'  ] = function($callNext, $a) {
    print("Around A (before)\n");
    $result = $callNext($a);
    print("Around A (after)\n");
    return $result;
};

echo $fn(new A());


use function GwangJinKim\Multidispatch\multidispatch;

// Interfaces & classes
interface Animal {}
class Dog implements Animal {}
class Cat implements Animal {}

// Create a dispatcher
$battle = multidispatch();

// Register primary methods
$battle[[Dog::class, Dog::class], ':primary']   = fn($a, $b) => "Dog vs Dog: Bark!";
$battle[[Animal::class, Animal::class], ':primary'] = fn($a, $b) => "Generic animal fight";

// Before and after hooks
$battle[[Dog::class, Dog::class], ':before'] = fn($a, $b) => print("Sniffing each other\n");
$battle[[Dog::class, Dog::class], ':after']  = fn($a, $b) => print("Wagging tails\n");

// Around (wrapping the dispatch, controlling the flow)
$battle[[Animal::class, Animal::class], ':around'] = function($callNext, $a, $b) {
    print("Arena lights up\n");
    $result = $callNext($a, $b);
    print("Audience cheers\n");
    return $result;
};

// Run dispatch!
echo $battle(new Dog(), new Dog());
echo $battle(new Cat(), new Dog());


use function GwangJinKim\Multidispatch\multidispatch;

// Define interfaces and classes
interface IA {}
class CA implements IA {}

// Make a dispatcher
$fn = multidispatch();

// :before hook (runs before primary)
$fn[[[CA::class], ':before']] = function($a) {
    echo "Logging before CA\n";
};
// :primary (main logic)
$fn[[[CA::class], ':primary']] = function($a) {
    echo "Handling CA\n";
    return "RESULT";
};
// :after hook (runs after primary)
$fn[[[CA::class], ':after']] = function($a) {
    echo "Cleanup after CA\n";
};
// :around wrapper (can see and control the whole call chain)
$fn[[[CA::class], ':around']] = function($callNext, $a) {
    echo "Start outer transaction\n";
    $result = $callNext($a); // This calls before/primary/after
    echo "End outer transaction\n";
    return "[[[$result]]]";
};

echo $fn(new CA()); // Try it out!

Logging before CA
Start outer transaction
Handling CA
Cleanup after CA
End outer transaction
[[[RESULT]]]
bash
    sudo apt update
    sudo apt install -y php php-cli php-xml php-zip php-curl phpunit composer
    
powershell
    scoop install php composer