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


$dispatcher = new Anddye\EventDispatcher\EventDispatcher();

// add listeners for when a user signed up event is dispatched
$dispatcher->addListener('UserRegistered', new App\Listeners\SendSignedUpEmail());
$dispatcher->addListener('UserRegistered', new App\Listeners\UpdateLastSignedInDate());

// create a user somehow
$user = new App\Models\User();
// ...

// create the user signed up event and dispatch it
$dispatcher->dispatch(new App\Events\UserSignedUp($user));

namespace App\Events;

use Anddye\EventDispatcher\Events\AbstractEvent;

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

namespace App\Listeners;

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

class SendSignedUpEmail extends AbstractListener
{
    public function handle(EventInterface $event): void
    {
        // TODO: This is where you would send the signed up email to the user!
    }
}