PHP code example of mepihindeveloper / php-event-dispatcher
1. Go to this page and download the library: Download mepihindeveloper/php-event-dispatcher 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/ */
mepihindeveloper / php-event-dispatcher example snippets
declare(strict_types = 1);
use mepihindeveloper\components\Event;
use mepihindeveloper\components\EventDispatcher;
use mepihindeveloper\components\interfaces\ListenerInterface;
use mepihindeveloper\components\ListenerProvider;
use Psr\EventDispatcher\StoppableEventInterface;
s Event2Listener1 implements ListenerInterface {
public function process(StoppableEventInterface $event) {
echo "Я " . get_class($this) . " слушаю событие " . get_class($event) . PHP_EOL;
}
}
class Event2Listener2 implements ListenerInterface {
public function process(StoppableEventInterface $event) {
echo "Я " . get_class($this) . " слушаю событие " . get_class($event) . PHP_EOL;
}
}
class AllEventsListener implements ListenerInterface {
public function process(StoppableEventInterface $event) {
echo "Я " . get_class($this) . " слушаю событие " . get_class($event) . PHP_EOL;
}
}
$event1 = new Event1;
$event2 = new Event2;
$event1Listener1 = new Event1Listener1;
$event2Listener1 = new Event2Listener1;
$event2Listener2 = new Event2Listener2;
$allEventsListener = new AllEventsListener;
$listenerProvider = new ListenerProvider;
$listenerProvider
->addListenerForEventType($event1Listener1, Event1::class)
->addListenerForEventType($event2Listener1, Event2::class)
->addListenerForEventType($event2Listener2, Event2::class)
->addListenerForEventType($allEventsListener);
$dispatcher = new EventDispatcher($listenerProvider);
$dispatcher->dispatch($event1);
$dispatcher->dispatch($event2);