Choosing approach to events

There many approaches to events.

Event class hierarchy

This approach plays nicely with PSR-14.

  • An event has no name except its class name.
  • Like any class, an event could be extended from another event and/or implement one or multiple interfaces.
  • You are able to add listeners for the event itself, any of its parents, any of its interfaces.
  • In order to listen to a group of events with a single listener, all these events could implement a dedicated interface. For example, all routing events could implement RoutingEvent. Order of execution, in this case, could be class listeners, parents hierarchy listeners, interfaces listeners. Order of interfaces is random.

Named events

  • Each event has a name.
  • You can subscribe to an event by name.
  • Listening to a group of events at once isn’t possible.

Named events and wildcards

  • Each event has a name.
  • You can subscribe to an event by name.
  • You can subscribe to a group of events by using a wildcard match i.e. routing.*.


  1. Are there more approaches?
  2. What benefits and cons do you see in each variant?
  3. Is there a real use-case for listening to multiple events at once except debug panel that could be done differently?

How about showing examples of Yii2’s events vs Yii3’s proposal?
Then show what can be done with each other and what one can do and the other cannot, with clear code examples.

This way it would make things super clear for anyone and we can take it from there.

Yii 2.0:

$event = new MessageEvent;
$event->message = $message;
$this->trigger(self::EVENT_MESSAGE_SENT, $event);

// ...

Event::on(Foo::className(), Foo::EVENT_MESSAGE_SENT, function (MessageEvent $event) {
    // event handling logic

You don’t have to get any extra components.


$dispatcher->dispatch(new MessageEvent($message));

// ...

$provider->attach(function (MessageEvent $event) {
    // log events here

You have to get dispatcher and provider via DI auto-wiring.

Both event systems are able to do more or less the same things.