0Pricing
PHP Academy · Lesson

EventDispatcher: Decoupled Events

Dispatch and listen to events using Symfony's EventDispatcher.

EventDispatcher: Decoupled Events is a free PHP Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the PHP Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What is EventDispatcher?

Symfony EventDispatcher implements the Observer pattern. Code dispatches events; listeners react to them — without the dispatcher knowing anything about the listeners.

Installing

Install the component.

$ composer require symfony/event-dispatcher

Creating an Event

An event is a plain PHP class, often extending Event (optional) and carrying data.

<?php
use Symfony\Contracts\EventDispatcher\Event;

class UserRegisteredEvent extends Event
{
    public const NAME = "user.registered";

    public function __construct(public readonly User $user) {}
}

Creating a Listener

A listener is a callable or object that reacts to a specific event.

<?php
class SendWelcomeEmailListener
{
    public function __invoke(UserRegisteredEvent $event): void
    {
        mail($event->user->email, "Welcome!", "Thanks for joining!");
    }
}

Registering Listeners

Attach listeners to the dispatcher.

<?php
use Symfony\Component\EventDispatcher\EventDispatcher;

$dispatcher = new EventDispatcher();
$dispatcher->addListener(
    UserRegisteredEvent::NAME,
    new SendWelcomeEmailListener()
);

Dispatching an Event

Dispatch the event — all registered listeners are called in priority order.

<?php
$user = new User("alice@example.com");
$dispatcher->dispatch(new UserRegisteredEvent($user), UserRegisteredEvent::NAME);

Listener Priority

Pass a priority integer as the third argument to addListener(). Higher numbers run first (default: 0).

<?php
$dispatcher->addListener("user.registered", $logListener, 10);   // runs first
$dispatcher->addListener("user.registered", $emailListener, 0);  // runs second

Event Subscribers

An event subscriber is a class that self-registers its listeners by implementing EventSubscriberInterface.

<?php
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class UserSubscriber implements EventSubscriberInterface
{
    public static function getSubscribedEvents(): array {
        return [
            UserRegisteredEvent::NAME => "onUserRegistered",
        ];
    }

    public function onUserRegistered(UserRegisteredEvent $event): void {
        // ...
    }
}

Stopping Propagation

Call $event->stopPropagation() inside a listener to prevent subsequent listeners from running.

In Laravel

Laravel has its own event system. Dispatch with event(new UserRegistered($user)). Register listeners in EventServiceProvider or use the #[Listen] attribute.

Benefits of Event-Driven Architecture

Events decouple components: the registration service does not import the email service. New behaviors (logging, analytics) can be added by registering new listeners without modifying existing code.

Summary

Create event classes with data, attach listeners with addListener(), and dispatch with $dispatcher->dispatch(). Use subscribers for self-registering listener groups.

Quick Check

How do you stop subsequent listeners from running?

Frequently asked questions

Is the “EventDispatcher: Decoupled Events” lesson free?

Yes — the full text of “EventDispatcher: Decoupled Events” is free to read here on the web, and the PHP Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the PHP Academy course, upgrade to CoddyKit PRO.

What will I learn in “EventDispatcher: Decoupled Events”?

Dispatch and listen to events using Symfony's EventDispatcher. You practise PHP Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start PHP Academy?

No prior experience is required. PHP Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “EventDispatcher: Decoupled Events” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this PHP Academy lesson?

Yes. Every PHP Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. HttpFoundation: Request and Response
  2. DependencyInjection Container
  3. EventDispatcher: Decoupled Events
  4. Console Component: CLI Commands
← Back to PHP Academy