PHP code example of dhii / event-interface

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

    

dhii / event-interface example snippets



use Dhii\Events\Event\StoppableEventFactoryInterface;

/* @var $factory StoppableEventFactoryInterface */
$event = $factory->make('my_event', ['key' => 'value']);
echo $event->getName(); // 'my_event'
echo $event->getParam('key'); //  'value'

$params = $event->getParams();
var_dump($params); // ['key' => 'value']

$params['hello'] = 'world';
$event->setParams($params);
echo $event->getParam('hello'); // 'world'


use Psr\EventDispatcher\EventDispatcherInterface;
use Dhii\Events\Event\EventInterface;
use Dhii\Events\Event\StoppableEventFactoryInterface;
use Dhii\Events\Listener\AddListenerCapableInterface;

/* @var $dispatcher EventDispatcherInterface */
/* @var $factory StoppableEventFactoryInterface */
/* @var $listenerMap AddListenerCapableInterface */

// First listener will change a value and stop propagation
$listenerMap->addListener('my_event', function (EventInterface $event) {
    $params = $event->getParams();
    $params['key'] = 'other_value';
    $event->setParams($params);
    $event->stopPropagation();
}, 1);

// Second listener is never run, therefore the value does not change again
$listenerMap->addListener('my_event', function (EventInterface $event) {
    $params = $event->getParams();
    $params['key'] = 'yet another value';
    $event->setParams($params);
}, 2);

$event = $factory->make('my_event', ['key' => 'value']);
$dispatcher->dispatch($event);
echo $event->getParam('key'); // 'other_value'