PHP code example of viloveul / event

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

    

viloveul / event example snippets




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());