PHP code example of effectra / event-dispatcher

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

    

effectra / event-dispatcher example snippets


namespace YourNamespace\Events;

use Effectra\EventDispatcher\Event;

class CustomEvent extends Event
{
    // Your event properties and methods
}

namespace YourNamespace;

use Effectra\EventDispatcher\ListenerProvider;
use YourNamespace\Events\CustomEvent;

// Create a listener provider
$listenerProvider = new ListenerProvider();

// Register a listener for the CustomEvent
$listenerProvider->addListener(CustomEvent::class, function (CustomEvent $event) {
    // Handle the CustomEvent
});

// Dispatching the event
$event = new CustomEvent();
$dispatcher = new EventDispatcher($listenerProvider);
$dispatcher->dispatch($event);

namespace YourNamespace\Events;

use Effectra\EventDispatcher\Event;

class StoppableEvent extends Event
{
    public function process(): void
    {
        // Your event processing logic

        // Stop further propagation if a condition is met
        if ($condition) {
            $this->stopPropagation();
        }
    }
}