PHP code example of rudra / event-dispatcher

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

    

rudra / event-dispatcher example snippets


use Rudra\EventDispatcher\EventDispatcherFacade as Dispatcher;

Dispatcher::addListener('app.listener', [AppListener::class, 'onEvent']);
Dispatcher::addListener('app.closure', function () {
    Rudra::config()->set(["closure" => "closure"]);
});
Dispatcher::addListener('before', [new TestController(), 'before']);

Dispatcher::dispatch('app.listener', 123);

// For Closure listeners, dispatch returns the Closure itself
$closure = Dispatcher::dispatch('app.closure');
$closure();

Dispatcher::dispatch('before');

Dispatcher::attachObserver("before", [TestController::class, "before"]);
Dispatcher::attachObserver("closure", ['closure', function () {
    Rudra::config()->set(['closure' => "closure"]);
}]);

$test = new TestController();
Dispatcher::attachObserver("subscriberObject", [$test, "subscriberObject"], 123);

Dispatcher::detachObserver("before", TestController::class);

Dispatcher::notify("before");
Dispatcher::notify("closure");
Dispatcher::notify("subscriberObject");

Dispatcher::getListeners();
Dispatcher::getObservers();