Class EventListener
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:
- The explicit
$methodargument when provided. __invoke()when present on the class.- A method name inferred from the event name (e.g.
Order.afterPlace→onOrderAfterPlace).
The attribute is repeatable, allowing a single method or class to listen to multiple events.
Examples
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 {}
Method Summary
-
__construct() public
Initializes an event listener attribute definition.
Method Detail
__construct() ¶ public
__construct(string $event, int|null $priority = null, string|null $method = null, string|null $manager = null)
Initializes an event listener attribute definition.
Parameters
-
string$event Event name to listen to (e.g.
'Order.afterPlace').-
int|null$priority optional Listener priority. When null, the receiving event manager's default priority is used at connection time.
-
string|null$method optional Explicit method name to use as the listener callable. Only applicable for class-level attributes; ignored when the attribute is placed on a method.
-
string|null$manager optional Named event manager to register the listener on. When null, the connector's primary event manager is used.