Skip to content

Refactor customized async event listener

Compare
Choose a tag to compare
@hhxsv5 hhxsv5 released this 15 Aug 07:55
  • Remove method:Listener::__construct(Event $event).
  • Change method:Listener::handle() to Listener::handle(Event $event).
  • Return false to stop propagating the event to subsequent listeners in Listener::handle().

Before:

abstract class Listener
{
    protected $event;

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

    /**
     * The logic of handling event
     * @return void
     */
    abstract public function handle();
}

Now:

abstract class Listener
{
    /**
     * The logic of handling event
     * @param Event $event
     * @return mixed
     */
    abstract public function handle(Event $event);
}