Declares an event listener for a class or method using PHP attributes.
When placed on a method, registers that method as the listener callable.
When placed on a class, resolves the callable using the following priority:
$method argument when provided.__invoke() when present on the class.Order.afterPlace → onOrderAfterPlace).The attribute is repeatable, allowing a single method or class to listen to multiple events.
Method-level (most common):
#[EventListener('Order.afterPlace')]
#[EventListener('Order.afterCancel', priority: 20)]
public function updateMetrics(EventInterface $event): void {}
Class-level with explicit method:
#[EventListener('Order.afterPlace', method: 'handleOrder')]
class OrderListener {}
Class-level with invokable class:
#[EventListener('Order.afterPlace')]
class OrderListener {
public function __invoke(EventInterface $event): void {}
}
Target a named event manager:
#[EventListener('Order.afterPlace', manager: 'orders')]
public function updateOrderMetrics(EventInterface $event): void {}