@@ -69,17 +69,6 @@ An :class:`Symfony\\Contracts\\EventDispatcher\\Event` instance is also
6969created and passed to all of the listeners. As you'll see later, the ``Event ``
7070object itself often contains data about the event being dispatched.
7171
72- Naming Conventions
73- ..................
74-
75- The unique event name can be any string, but optionally follows a few
76- naming conventions:
77-
78- * Use only lowercase letters, numbers, dots (``. ``) and underscores (``_ ``);
79- * Prefix names with a namespace followed by a dot (e.g. ``order.* ``, ``user.* ``);
80- * End names with a verb that indicates what action has been taken (e.g.
81- ``order.placed ``).
82-
8372Event Names and Event Objects
8473.............................
8574
@@ -259,7 +248,7 @@ system flexible and decoupled.
259248Creating an Event Class
260249.......................
261250
262- Suppose you want to create a new event - `` order.placed `` - that is dispatched
251+ Suppose you want to create a new event that is dispatched
263252each time a customer orders a product with your application. When dispatching
264253this event, you'll pass a custom event instance that has access to the placed
265254order. Start by creating this custom event class and documenting it::
@@ -270,19 +259,12 @@ order. Start by creating this custom event class and documenting it::
270259 use Symfony\Contracts\EventDispatcher\Event;
271260
272261 /**
273- * The order.placed event is dispatched each time an order is created
274- * in the system.
262+ * This event is dispatched each time an order
263+ * is placed in the system.
275264 */
276- class OrderPlacedEvent extends Event
265+ final class OrderPlacedEvent extends Event
277266 {
278- public const NAME = 'order.placed';
279-
280- protected $order;
281-
282- public function __construct(Order $order)
283- {
284- $this->order = $order;
285- }
267+ public function __construct(private Order $order) {}
286268
287269 public function getOrder(): Order
288270 {
@@ -318,10 +300,10 @@ of the event to dispatch::
318300
319301 // creates the OrderPlacedEvent and dispatches it
320302 $event = new OrderPlacedEvent($order);
321- $dispatcher->dispatch($event, OrderPlacedEvent::NAME );
303+ $dispatcher->dispatch($event);
322304
323305Notice that the special ``OrderPlacedEvent `` object is created and passed to
324- the ``dispatch() `` method. Now, any listener to the ``order.placed ``
306+ the ``dispatch() `` method. Now, any listener to the ``OrderPlacedEvent::class ``
325307event will receive the ``OrderPlacedEvent ``.
326308
327309.. _event_dispatcher-using-event-subscribers :
@@ -340,7 +322,7 @@ events it should subscribe to. It implements the
340322interface, which requires a single static method called
341323:method: `Symfony\\ Component\\ EventDispatcher\\ EventSubscriberInterface::getSubscribedEvents `.
342324Take the following example of a subscriber that subscribes to the
343- ``kernel.response `` and ``order.placed `` events::
325+ ``kernel.response `` and ``OrderPlacedEvent::class `` events::
344326
345327 namespace Acme\Store\Event;
346328
@@ -358,7 +340,7 @@ Take the following example of a subscriber that subscribes to the
358340 ['onKernelResponsePre', 10],
359341 ['onKernelResponsePost', -10],
360342 ],
361- OrderPlacedEvent::NAME => 'onStoreOrder ',
343+ OrderPlacedEvent::class => 'onPlacedOrder ',
362344 ];
363345 }
364346
@@ -372,8 +354,9 @@ Take the following example of a subscriber that subscribes to the
372354 // ...
373355 }
374356
375- public function onStoreOrder (OrderPlacedEvent $event)
357+ public function onPlacedOrder (OrderPlacedEvent $event): void
376358 {
359+ $order = $event->getOrder();
377360 // ...
378361 }
379362 }
@@ -417,14 +400,14 @@ inside a listener via the
417400
418401 use Acme\Store\Event\OrderPlacedEvent;
419402
420- public function onStoreOrder (OrderPlacedEvent $event)
403+ public function onPlacedOrder (OrderPlacedEvent $event): void
421404 {
422405 // ...
423406
424407 $event->stopPropagation();
425408 }
426409
427- Now, any listeners to ``order.placed `` that have not yet been called will
410+ Now, any listeners to ``OrderPlacedEvent::class `` that have not yet been called will
428411*not * be called.
429412
430413It is possible to detect if an event was stopped by using the
0 commit comments