March 25, 2019 Javier Eguiluz

In Symfony 3.3, we made the class optional for named services and started recommending to use the fully-qualified class name (FQCN) as the service ID. This makes config more intuitive and helps you avoid thinking about arbitrary strings to name services.

Following the same idea, in Symfony 4.3 we've changed the signature of the EventDispatcherInterface::dispatch() method:

1 2 3 4 5 6 7 8 9 // ... $order = new Order (); $newOrderEvent = new OrderPlacedEvent ( $order ); // Before $dispatcher -> dispatch ( OrderEvents :: NEW_ORDER , $newOrderEvent ); // After $dispatcher -> dispatch ( $newOrderEvent , OrderEvents :: NEW_ORDER );

Although this change looks minor, it helps simplify the rest of the app code. For starters, you can subscribe to events using the FQCN of the event:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 class StoreSubscriber implements EventSubscriberInterface { public static function getSubscribedEvents () { // Before return [ OrderEvents :: NEW_ORDER => 'onStoreOrder' , ]; // After return [ OrderPlacedEvent :: class => 'onStoreOrder' , ]; } // ... }

Moreover, the event name is now optional in the dispatch() method, so you can pass just the event object:

1 2 3 4 5 // Before $dispatcher -> dispatch ( OrderEvents :: NEW_ORDER , $newOrderEvent ); // After $dispatcher -> dispatch ( $newOrderEvent );

In summary, the new dispatch() signature allows you to develop code based on pure PHP classes instead of inventing arbitrary strings to name events.