PHP code example of gobline / mediator

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

    

gobline / mediator example snippets


use Gobline\Mediator\EventDispatcher;

$dispatcher = new EventDispatcher();

use Gobline\Mediator\EventSubscriberInterface;

class FooSubscriber implements EventSubscriberInterface
{
    public function onFooEvent()
    {
        // ... do something
    }

    public function getSubscribedEvents()
    {
        return ['fooEvent' => 'onFooEvent'];
    }
}

$subscriber = new FooSubscriber();
$dispatcher->addSubscriber($subscriber);

class FooListener
{
    public function onFooEvent()
    {
        // ... do something
    }
}

$listener = new FooListener();
$dispatcher->addListener($listener, ['fooEvent' => 'onFooEvent']);

$dispatcher->addListener(
    function() { return new FooListener(); },
    ['fooEvent' => 'onFooEvent']);

$dispatcher->dispatch('fooEvent');

$dispatcher->dispatch('fooEvent', ['foo' => 'bar']);

class FooListener
{
    public function onFooEvent(array $data)
    {
        // ... do something with $data['foo']
    }
}