PHP code example of flying / handlers-list

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

    

flying / handlers-list example snippets


interface MyHandler {
    public function doSomething(): void;
}

class Foo implements MyHandler {

}

class Bar implements MyHandler {

}

class Baz extends Bar {

}

// Creating list of handlers
$handlers = new HandlersList([
    new Foo(), 
    new Bar(),
    new Baz(),
], MyHandler::class);

// ... later in code ...
foreach($handlers as $handler) {
    // We can be sure that $handler is of type MyHandler::class
    $handler->doSomething(); 
}

interface MyHandler {
    public function name(): string;
}

class A implements MyHandler {
    public function name(): string {
        return 'A';
    }
}

class B implements MyHandler, PrioritizedHandlerInterface {
    public function name(): string {
        return 'B';
    }

    public function getHandlerPriority(): int {
        return 10;
    }
}
 
$handlers = new HandlersList([
    new A(), 
    new B(),
], MyHandler::class);

foreach($handlers as $handler) {
    echo $handler->name() . ' '; 
}