PHP code example of aesonus / events

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

    

aesonus / events example snippets


$dispatcher = new Dispatcher();

class MyEvent extends Event { }

    ...
    public function handle(EventInterface $event): void { }
}

$event = new MyEvent();

$event->attach(new MyListener());

$event->attach(new MyListener(), 3);

$listeners = [new MyListener(),new MyListener(),new MyListener()];
$event->attach($listeners);

$listeners = [new MyListener(),new MyListener(),new MyListener()];

//They all get a priority of 3
$event->attach($listeners, 3);

$listenersA = [new MyListenerA(),new MyListenerB()];
$event->attach($listenersA);

$listenersB = [new MyListenerC(),new MyListenerD()];
$event->attach($listenersB);

$listeners = [new MyListener(),[new MyListener(), 2],new MyListener()];

//They all get a priority of 3, except for the the listener at index 1
$event->attach($listeners, 3);

$dispatcher->register($event);

$dispatcher->dispatch(MyEvent::class);

$event->dispatch();

try {
    $event->dispatch(); // This will throw an exception from one of the listeners
} catch (ResumableException $ex) {
    //do stuff to make all better
}

//Resume the dispatch
$event->dispatch();
...

$event->reset();