PHP code example of next / event

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

    

next / event example snippets


use Next\Event\Contract\EventListenerInterface;

class UserStatusListener implements EventListenerInterface
{
    /**
    * 返回该监听器监听的事件
    * @return string[]
    */
    public function listen():array {
        return [
            \App\Events\UserRegistered::class,
        ];
    }

    /**
    * 触发事件后的处理
    * @param object $event
    */
    public function process(object $event): void
    {
        $event->user = false;
    }
    
    /**
    * 监听器优先级
    * 如果一个事件被多个监听器监听,那么执行顺序可以通过该方法调整
    * 优先级数字越大,优先级越高,越先执行
    * @return int
    */
    public function getPriority(): int 
    {
        return 0;
    }
}

class UserRegistered
{
    public $user;

    public function __construct($user)
    {
        $this->user = $user;
    }
}

$listenerProvider = new ListenerProvider();
$listenerProvider->addListener(new UserStatusListener());

$dispatcher = new \Next\Event\EventDispatcher($listenerProvider);

$user = User::find(1);

$event = $dispatcher->dispatch(new UserRegistered($user));

class UserRegistered implements StoppableEventInterface
{
    public $user;

    public function __construct($user)
    {
        $this->user = $user;
    }

    public function isPropagationStopped() : bool 
    {
        return true;
    }
}