Skip to content
PHP Ecosystem
Explore

Event

API reference › yii › base › Event

classyii\base\Event
ExtendsBaseObject
ImplementsConfigurable
Since2.0
Sourceframework/base/Event.php
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.

PropertyTypeDescriptionDefined by
$datamixedThe data that is passed to Component::on() when attaching an event handler. Default: null.Event
$handledboolWhether the event is handled. Default: false.Event
$namestringThe event name. Default: null.Event
$senderobject|nullThe sender of this event. Default: null.Event
MethodDescriptionDefined 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() staticReturns the fully qualified name of this class.BaseObject
hasHandlers() staticReturns 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() staticDetaches an event handler from a class-level event.Event
offAll() staticDetaches all registered class-level event handlers.Event
on() staticAttaches an event handler to a class-level event.Event
trigger() staticTriggers a class-level event.Event
public mixed $data = null

The data that is passed to Component::on() when attaching an event handler.

Note that this varies according to which event handler is currently executing.

public bool $handled = false

Whether 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.

public string $name = null

The event name.

This property is set by Component::trigger() and trigger(). Event handlers may use this property to check what event it is handling.

public object|null $sender = null

The 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.

public static function hasHandlers(string|object $class, string $name): bool

Returns 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

ParameterTypeDescription
$classstring|objectThe object or the fully qualified class name specifying the class-level event.
$namestringThe event name.

Returns

bool: Whether there is any handler attached to the event.

public static function off(string $class, string $name, callable|null $handler = null): bool

Detaches 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

ParameterTypeDescription
$classstringThe fully qualified class name from which the event handler needs to be detached.
$namestringThe event name.
$handlercallable|nullThe 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

public static function offAll(): mixed

Detaches all registered class-level event handlers.

Returns

mixed

Since

2.0.10

See also

public static function on(string $class, string $name, callable $handler, mixed $data = null, bool $append = true): mixed

Attaches 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

ParameterTypeDescription
$classstringThe fully qualified class name to which the event handler needs to attach.
$namestringThe event name.
$handlercallableThe event handler.
$datamixedThe 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.
$appendboolWhether 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

public static function trigger(string|object $class, string $name, yii\base\Event|null $event = null): mixed

Triggers 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.

Parameters

ParameterTypeDescription
$classstring|objectThe object or the fully qualified class name specifying the class-level event.
$namestringThe event name.
$eventEvent|nullThe event parameter. If not set, a default Event object will be created.

Returns

mixed