PHP code example of comphp / events

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

    

comphp / events example snippets



Neuron\Events\EventDispatcher;
use Neuron\Events\AbstractEvent;

// Define an event
class UserRegisteredEvent extends AbstractEvent
{
    public function __construct(private string $username)
    {
        parent::__construct(['username' => $username]);
    }
}

// Initialize the dispatcher
$dispatcher = new EventDispatcher();

// Register a listener
$dispatcher->listen(UserRegisteredEvent::class, function (UserRegisteredEvent $event) {
    echo "User registered: " . $event->getPayload()['username'] . "\n";
});

// Dispatch the event
$dispatcher->dispatch(new UserRegisteredEvent('Tim'));