PHP code example of switon / event

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

    

switon / event example snippets


use Psr\EventDispatcher\EventDispatcherInterface;
use Switon\Core\Attribute\Autowired;
use Switon\Eventing\Attribute\EventListener;

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

class UserService
{
    #[Autowired] protected EventDispatcherInterface $eventDispatcher;

    public function register(string $email): void
    {
        $this->eventDispatcher->dispatch(new UserRegistered(123, $email));
    }
}

class UserListener
{
    #[EventListener]
    public function onUserRegistered(UserRegistered $event): void
    {
        // react to the event
    }
}