When a class-level event is triggered, event handlers attached
to that class and all parent classes will be invoked.
For example, the following code attaches an event handler to ActiveRecord's
afterInsert event:
php
Event::on(ActiveRecord::className(), ActiveRecord::EVENT_AFTER_INSERT, function ($event) {
Yii::trace(get_class($event->sender) . ' is inserted.');
});
The handler will be invoked for EVERY successful ActiveRecord insertion.
For more details about how to declare an event handler, please refer to [[Component::on()]].
public static on ( string $class, string $name, callable $handler, mixed $data = null, boolean $append = true ) | ||
$class | string | the fully qualified class name to which the event handler needs to attach. |
$name | string | the event name. |
$handler | callable | the event handler. |
$data | mixed | the data to be passed to the event handler when the event is triggered. When the event handler is invoked, this data can be accessed via [[Event::data]]. |
$append | boolean | whether to append new event handler to the end of the existing handler list. If `false`, the new handler will be inserted at the beginning of the existing handler list. |