Event
API reference › yii › base › Event
10 subclasses
Event is the base class for all event classes.
It encapsulates the parameters associated with an event.
The $sender property describes who raises the event.
And the $handled property indicates if the event is handled.
If an event handler sets $handled to be true, the rest of the
uninvoked handlers will no longer be called to handle the event.
Additionally, when attaching an event handler, extra data may be passed
and be available via the $data property when the event handler is invoked.
For more details and usage information on Event, see the guide article on events.
Properties
Section titled “Properties”| Property | Type | Description | Defined by |
|---|---|---|---|
$data | mixed | The data that is passed to Component::on() when attaching an event handler. Default: null. | Event |
$handled | bool | Whether the event is handled. Default: false. | Event |
$name | string | The event name. Default: null. | Event |
$sender | object| | The sender of this event. Default: null. | Event |
Methods
Section titled “Methods”| Method | Description | Defined by |
|---|---|---|
__construct() | Constructor. | BaseObject |
__call() | Calls the named method which is not a class method. | BaseObject |
__get() | Returns the value of an object property. | BaseObject |
__isset() | Checks if a property is set, i.e. defined and not null. | BaseObject |
__set() | Sets value of an object property. | BaseObject |
__unset() | Sets an object property to null. | BaseObject |
canGetProperty() | Returns a value indicating whether a property can be read. | BaseObject |
canSetProperty() | Returns a value indicating whether a property can be set. | BaseObject |
className() static | Returns the fully qualified name of this class. | BaseObject |
hasHandlers() static | Returns a value indicating whether there is any handler attached to the specified class-level event. | Event |
hasMethod() | Returns a value indicating whether a method is defined. | BaseObject |
hasProperty() | Returns a value indicating whether a property is defined. | BaseObject |
init() | Initializes the object. | BaseObject |
off() static | Detaches an event handler from a class-level event. | Event |
offAll() static | Detaches all registered class-level event handlers. | Event |
on() static | Attaches an event handler to a class-level event. | Event |
trigger() static | Triggers a class-level event. | Event |
Property details
Section titled “Property details”$data
Section titled “$data ”public mixed $data = nullThe data that is passed to Component::on() when attaching an event handler.
Note that this varies according to which event handler is currently executing.
$handled
Section titled “$handled ”public bool $handled = falseWhether the event is handled.
Defaults to false.
When a handler sets this to be true, the event processing will stop and
ignore the rest of the uninvoked event handlers.
$name
Section titled “$name ”public string $name = nullThe event name.
This property is set by Component::trigger() and trigger().
Event handlers may use this property to check what event it is handling.
$sender
Section titled “$sender ”public object|null $sender = nullThe sender of this event.
If not set, this property will be
set as the object whose trigger() method is called.
This property may also be a null when this event is a
class-level event which is triggered in a static context.
Method details
Section titled “Method details”hasHandlers()
Section titled “hasHandlers() ”public static function hasHandlers(string|object $class, string $name): boolReturns a value indicating whether there is any handler attached to the specified class-level event.
Note that this method will also check all parent classes to see if there is any handler attached to the named event.
Parameters
| Parameter | Type | Description |
|---|---|---|
$class | string| | The object or the fully qualified class name specifying the class-level event. |
$name | string | The event name. |
Returns
bool: Whether there is any handler attached to the event.
off()
Section titled “off() ”public static function off(string $class, string $name, callable|null $handler = null): boolDetaches an event handler from a class-level event.
This method is the opposite of on().
Note: in case wildcard pattern is passed for class name or event name, only the handlers registered with this wildcard will be removed, while handlers registered with plain names matching this wildcard will remain.
Parameters
| Parameter | Type | Description |
|---|---|---|
$class | string | The fully qualified class name from which the event handler needs to be detached. |
$name | string | The event name. |
$handler | callable| | The event handler to be removed. If it is null, all handlers attached to the named event will be removed. |
Returns
bool: Whether a handler is found and detached.
See also
offAll()
Section titled “offAll() ”public static function offAll(): mixedDetaches all registered class-level event handlers.
public static function on(string $class, string $name, callable $handler, mixed $data = null, bool $append = true): mixedAttaches an event handler to a class-level event.
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:
Event::on(ActiveRecord::class, ActiveRecord::EVENT_AFTER_INSERT, function ($event) { Yii::trace(get_class($event->sender) . ' is inserted.');});The handler will be invoked for EVERY successful ActiveRecord insertion.
Since 2.0.14 you can specify either class name or event name as a wildcard pattern:
Event::on('app\models\db\*', '*Insert', function ($event) { Yii::trace(get_class($event->sender) . ' is inserted.');});For more details about how to declare an event handler, please refer to Component::on().
Parameters
| Parameter | Type | Description |
|---|---|---|
$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 | bool | 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. |
Returns
mixed
See also
trigger()
Section titled “trigger() ”public static function trigger(string|object $class, string $name, yii\base\Event|null $event = null): mixedTriggers a class-level event.
This method will cause invocation of event handlers that are attached to the named event for the specified class and all its parent classes.