1. Go to this page and download the library: Download semperton/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/ */
semperton / events example snippets
use Semperton\Events\ListenerProvider;
interface EventInterface{}
final class TestEvent implements EventInterface
{
public $message = '';
}
$provider = new ListenerProvider();
$provider->addListener(TestEvent::class, function (TestEvent $event) {
$event->message .= ' World';
});
$provider->addListener(EventInterface::class, function (EventInterface $event) {
$event->message = 'Hello';
}, -1); // gets called first, because of higher priority (negatives allowed)
$myListener = function (TestEvent $event) {};
$provider->addListener(TestEvent::class, $myListener, 7);
$provider->removeListener(TestEvent::class, $myListener, 7); // priority must match too
use Semperton\Events\EventDispatcher;
// using the previously created ListenerProvider
$dispatcher = new EventDispatcher($provider);
$event = new TestEvent();
/** @var TestEvent */
$dispatchedEvent = $dispatcher->dispatch($event);
$dispatchedEvent === $event; // true
$dispatchedEvent->message; // 'Hello World'
$resolver = static function (string $class): object {
return new $class();
};
$listener = new DelegateListener($resolver, Service::class, 'method');
// $container is a Psr\Container\ContainerInterface
$listener = new DelegateListener([$container, 'get'], Service::class, 'method');
$provider->addListener(TestEvent::class, $listener);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.