PHP code example of andrewdyer / event-dispatcher

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

    

andrewdyer / event-dispatcher example snippets


namespace App\Events;

use AndrewDyer\EventDispatcher\Events\AbstractEvent;

class UserRegistered extends AbstractEvent
{
    public function getName(): string
    {
        return 'UserRegistered';
    }
}

namespace App\Listeners;

use AndrewDyer\EventDispatcher\Events\EventInterface;
use AndrewDyer\EventDispatcher\Listeners\AbstractListener;

class SendRegistrationEmail extends AbstractListener
{
    public function handle(EventInterface $event): void
    {
        // Send welcome email to user...
    }
}

use AndrewDyer\EventDispatcher\EventDispatcher;

$dispatcher = new EventDispatcher();

use App\Listeners\SendRegistrationEmail;

$dispatcher->addListener('UserRegistered', new SendRegistrationEmail());

use App\Events\UserRegistered;

$dispatcher->dispatch(new UserRegistered());