/event

trigger or dispatching an Event listener

Primary LanguagePHP

Build Status Total Downloads Latest Stable Version

Installation

make sure your php version > 7.0

composer require viloveul/event

How

require __DIR__ . '/vendor/autoload.php';

class MyEvent
{
    /**
     * @var string
     */
    public $name = 'foo';
}

class MyListener
{
    /**
     * @param MyEvent $event
     */
    public function __invoke(MyEvent $event)
    {
        if ($event->name === 'foo') {
            throw new Exception("foo");
        } else {
            throw new Exception("bar");
        }
    }
}

$provider = new Viloveul\Event\Provider();
$provider->addListener(new MyListener());

$dispatcher = new Viloveul\Event\Dispatcher($provider);
$dispatcher->dispatch(new MyEvent());