# Event

> Event is the base class for all event classes.

[API reference](/docs/api/index.md) › [yii](/docs/api/yii/index.md) › [base](/docs/api/yii/base/index.md) › Event

| class | `yii\base\Event` |
| --- | --- |
| Extends | [`BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| Implements | [`Configurable`](/docs/api/yii/base/configurable/index.md) |
| Since | 2.0 |
| Source | [`framework/base/Event.php`](https://github.com/yiisoft/yii2/blob/22.0/framework/base/Event.php#L30) |

10 subclasses:

- [`ActionEvent`](/docs/api/yii/base/actionevent/index.md)
- [`ModelEvent`](/docs/api/yii/base/modelevent/index.md)
- [`ViewEvent`](/docs/api/yii/base/viewevent/index.md)
- [`WidgetEvent`](/docs/api/yii/base/widgetevent/index.md)
- [`yii\db\AfterSaveEvent`](/docs/api/yii/db/aftersaveevent/index.md)
- [`yii\i18n\MissingTranslationEvent`](/docs/api/yii/i18n/missingtranslationevent/index.md)
- [`yii\mail\MailEvent`](/docs/api/yii/mail/mailevent/index.md)
- [`yii\web\ErrorHandlerRenderEvent`](/docs/api/yii/web/errorhandlerrenderevent/index.md)
- [`yii\web\UserEvent`](/docs/api/yii/web/userevent/index.md)
- [`yii2\extensions\localeurls\LanguageChangedEvent`](/docs/extensions/localeurls/reference/languagechangedevent/index.md)

Event is the base class for all event classes.

It encapsulates the parameters associated with an event.
The [`$sender`](/docs/api/yii/base/event/index.md#property-sender) property describes who raises the event.
And the [`$handled`](/docs/api/yii/base/event/index.md#property-handled) property indicates if the event is handled.
If an event handler sets [`$handled`](/docs/api/yii/base/event/index.md#property-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`](/docs/api/yii/base/event/index.md#property-data) property when the event handler is invoked.

For more details and usage information on Event, see the [guide article on events](/docs/guide/concept-events/index.md).

## Properties

| Property | Type | Description | Defined by |
| --- | --- | --- | --- |
| [`$data`](#property-data) | `mixed` | The data that is passed to [`Component::on()`](/docs/api/yii/base/component/index.md#method-on) when attaching an event handler. Default: `null`. | `Event` |
| [`$handled`](#property-handled) | `bool` | Whether the event is handled. Default: `false`. | `Event` |
| [`$name`](#property-name) | `string` | The event name. Default: `null`. | `Event` |
| [`$sender`](#property-sender) | `object\|null` | The sender of this event. Default: `null`. | `Event` |

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| <span id="method-__construct"></span>[`__construct()`](/docs/api/yii/base/baseobject/index.md#method-__construct) | Constructor. | [`BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__call"></span>[`__call()`](/docs/api/yii/base/baseobject/index.md#method-__call) | Calls the named method which is not a class method. | [`BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__get"></span>[`__get()`](/docs/api/yii/base/baseobject/index.md#method-__get) | Returns the value of an object property. | [`BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__isset"></span>[`__isset()`](/docs/api/yii/base/baseobject/index.md#method-__isset) | Checks if a property is set, i.e. defined and not null. | [`BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__set"></span>[`__set()`](/docs/api/yii/base/baseobject/index.md#method-__set) | Sets value of an object property. | [`BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__unset"></span>[`__unset()`](/docs/api/yii/base/baseobject/index.md#method-__unset) | Sets an object property to null. | [`BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-canGetProperty"></span>[`canGetProperty()`](/docs/api/yii/base/baseobject/index.md#method-canGetProperty) | Returns a value indicating whether a property can be read. | [`BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-canSetProperty"></span>[`canSetProperty()`](/docs/api/yii/base/baseobject/index.md#method-canSetProperty) | Returns a value indicating whether a property can be set. | [`BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-className"></span>[`className()`](/docs/api/yii/base/baseobject/index.md#method-className) static | Returns the fully qualified name of this class. | [`BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| [`hasHandlers()`](#method-hasHandlers) static | Returns a value indicating whether there is any handler attached to the specified class-level event. | `Event` |
| <span id="method-hasMethod"></span>[`hasMethod()`](/docs/api/yii/base/baseobject/index.md#method-hasMethod) | Returns a value indicating whether a method is defined. | [`BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-hasProperty"></span>[`hasProperty()`](/docs/api/yii/base/baseobject/index.md#method-hasProperty) | Returns a value indicating whether a property is defined. | [`BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-init"></span>[`init()`](/docs/api/yii/base/baseobject/index.md#method-init) | Initializes the object. | [`BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| [`off()`](#method-off) static | Detaches an event handler from a class-level event. | `Event` |
| [`offAll()`](#method-offAll) static | Detaches all registered class-level event handlers. | `Event` |
| [`on()`](#method-on) static | Attaches an event handler to a class-level event. | `Event` |
| [`trigger()`](#method-trigger) static | Triggers a class-level event. | `Event` |

## Property details

### $data <span id="property-data"></span>

```php
public mixed $data = null
```

The data that is passed to [`Component::on()`](/docs/api/yii/base/component/index.md#method-on) when attaching an event handler.

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

### $handled <span id="property-handled"></span>

```php
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.

### $name <span id="property-name"></span>

```php
public string $name = null
```

The event name.

This property is set by [`Component::trigger()`](/docs/api/yii/base/component/index.md#method-trigger) and [`trigger()`](/docs/api/yii/base/event/index.md#method-trigger).
Event handlers may use this property to check what event it is handling.

### $sender <span id="property-sender"></span>

```php
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.

## Method details

### hasHandlers() <span id="method-hasHandlers"></span>

```php
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**

| Parameter | Type | Description |
| --- | --- | --- |
| `$class` | `string\|object` | 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() <span id="method-off"></span>

```php
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()`](/docs/api/yii/base/event/index.md#method-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\|null` | 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**

- [`on()`](/docs/api/yii/base/event/index.md#method-on)

### offAll() <span id="method-offAll"></span>

```php
public static function offAll(): mixed
```

Detaches all registered class-level event handlers.

**Returns**

`mixed`

**Since**

2.0.10

**See also**

- [`on()`](/docs/api/yii/base/event/index.md#method-on)
- [`off()`](/docs/api/yii/base/event/index.md#method-off)

### on() <span id="method-on"></span>

```php
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()`](/docs/api/yii/base/component/index.md#method-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`](/docs/api/yii/base/event/index.md#property-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**

- [`off()`](/docs/api/yii/base/event/index.md#method-off)

### trigger() <span id="method-trigger"></span>

```php
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**

| Parameter | Type | Description |
| --- | --- | --- |
| `$class` | `string\|object` | The object or the fully qualified class name specifying the class-level event. |
| `$name` | `string` | The event name. |
| `$event` | [`Event`](/docs/api/yii/base/event/index.md)`\|null` | The event parameter. If not set, a default [`Event`](/docs/api/yii/base/event/index.md) object will be created. |

**Returns**

`mixed`