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.*
.
Questions
- Are there more approaches?
- What benefits and cons do you see in each variant?
- Is there a real use-case for listening to multiple events at once except debug panel that could be done differently?