PHP code example of phpdot / event

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

    

phpdot / event example snippets


use PHPdot\Event\Attribute\Listener;

final readonly class UserRegistered
{
    public function __construct(public int $userId, public string $email) {}
}

#[Listener(UserRegistered::class, order: 1)]
final class SendWelcomeEmail
{
    public function __construct(private MailerInterface $mailer) {}

    public function __invoke(UserRegistered $event): void
    {
        $this->mailer->send($event->email, 'Welcome!');
    }
}

use PHPdot\Event\Discovery\AttributeListenerDiscovery;
use PHPdot\Event\ListenerProvider;

$discovery = new AttributeListenerDiscovery([$appPath . '/Listeners', $srcPath]);
$provider = new ListenerProvider($container);
$provider->load($discovery->discover());

$dispatcher->dispatch(new UserRegistered(userId: 1, email: '[email protected]'));