PHP code example of foxphp / event-dispatcher

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

    

foxphp / event-dispatcher example snippets


// Create a new dispatcher

$dispatcher = Dispatcher::make(
    listeners: [
        Path\To\Event::class => [
            Path\To\Listener::class,
        ]
    ]
);

$dispatcher = new Dispatcher(
    listeners: [],
    log: []
);

// Add event with listeners
$dispatcher->add(
    event: Path\To\Event::class,
    listeners: [
        Path\To\Listener::class,
    ]
);

// Append listeners onto an event listener array
$dispatcher->append(
    event: Path\To\Event::class,
    listeners: [
        Path\To\Another\Listener::class,
    ]
);

// Dispatch
$dispatcher->dispatch(
    event: new Path\To\Event(),
    debug: true,
);

// Get debug log items
$dispatcher->log(); // []