PHP code example of weew / eventer

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

    

weew / eventer example snippets


$eventer = new Eventer();
$eventer->subscribe('event.name', function(IEvent $event) {
    echo $event->getName();
    // event.name
});
$eventer->dispatch('event.name');

$eventer = new Eventer();
$eventer->subscribe('event.name', function(IEvent $event) {
    var_dump($event->getData());
    // ['secret' => 'secret value']
    echo $event->get('secret');
    // secret value
});

$event = new GenericEvent('event.name', ['secret' => 'secret value']);
// or
$event = new GenericEvent('event.name');
$event->set('secret', 'secret value');

$eventer->dispatch($event);

class CustomEvent extends Event {
    public function getSecret() {
        return 'secret value';
    }
}

$eventer = new Eventer();
$eventer->subscribe(CustomEvent::class, function(CustomEvent $event) {
    echo $event->getSecret();
    // secret value
});

$eventer->dispatch(new CustomEvent());

$eventer = new Eventer();
$subscription = $eventer->subscribe('event.name', 'abstract.value');
$eventer->unsubscribe($subscription);

class CustomEvent extends Event {
    public function getSecret() {
        return 'secret value';
    }
}

class CustomEventSubscriber {
    public function handle(IEvent $event) {
        /** @var CustomEvent $event */
        echo $event->getSecret();
        // secret value
    }
}

$eventer = new Eventer();
$eventer->subscribe(CustomEvent::class, CustomEventSubscriber::class);
$eventer->dispatch(new CustomEvent());