# Component

> Component is the base class that implements the *property*, *event* and *behavior* features.

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

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

48 subclasses:

- [`Action`](/docs/api/yii/base/action/index.md)
- [`Controller`](/docs/api/yii/base/controller/index.md)
- [`ErrorHandler`](/docs/api/yii/base/errorhandler/index.md)
- [`Model`](/docs/api/yii/base/model/index.md)
- [`Request`](/docs/api/yii/base/request/index.md)
- [`Response`](/docs/api/yii/base/response/index.md)
- [`Security`](/docs/api/yii/base/security/index.md)
- [`Theme`](/docs/api/yii/base/theme/index.md)
- [`View`](/docs/api/yii/base/view/index.md)
- [`ViewRenderer`](/docs/api/yii/base/viewrenderer/index.md)
- [`Widget`](/docs/api/yii/base/widget/index.md)
- [`yii\caching\Cache`](/docs/api/yii/caching/cache/index.md)
- [`yii\data\BaseDataProvider`](/docs/api/yii/data/basedataprovider/index.md)
- [`yii\db\BatchQueryResult`](/docs/api/yii/db/batchqueryresult/index.md)
- [`yii\db\Command`](/docs/api/yii/db/command/index.md)
- [`yii\db\Connection`](/docs/api/yii/db/connection/index.md)
- [`yii\db\Migration`](/docs/api/yii/db/migration/index.md)
- [`yii\db\Query`](/docs/api/yii/db/query/index.md)
- [`yii\db\SqlTokenizer`](/docs/api/yii/db/sqltokenizer/index.md)
- [`yii\di\Container`](/docs/api/yii/di/container/index.md)
- [`yii\di\ServiceLocator`](/docs/api/yii/di/servicelocator/index.md)
- [`yii\filters\AccessRule`](/docs/api/yii/filters/accessrule/index.md)
- [`yii\i18n\Formatter`](/docs/api/yii/i18n/formatter/index.md)
- [`yii\i18n\GettextFile`](/docs/api/yii/i18n/gettextfile/index.md)
- [`yii\i18n\I18N`](/docs/api/yii/i18n/i18n/index.md)
- [`yii\i18n\Locale`](/docs/api/yii/i18n/locale/index.md)
- [`yii\i18n\MessageFormatter`](/docs/api/yii/i18n/messageformatter/index.md)
- [`yii\i18n\MessageSource`](/docs/api/yii/i18n/messagesource/index.md)
- [`yii\log\Dispatcher`](/docs/api/yii/log/dispatcher/index.md)
- [`yii\log\Logger`](/docs/api/yii/log/logger/index.md)
- [`yii\log\Target`](/docs/api/yii/log/target/index.md)
- [`yii\mail\BaseMailer`](/docs/api/yii/mail/basemailer/index.md)
- [`yii\mutex\Mutex`](/docs/api/yii/mutex/mutex/index.md)
- [`yii\rbac\BaseManager`](/docs/api/yii/rbac/basemanager/index.md)
- [`yii\rest\Serializer`](/docs/api/yii/rest/serializer/index.md)
- [`yii\test\Fixture`](/docs/api/yii/test/fixture/index.md)
- [`yii\validators\Validator`](/docs/api/yii/validators/validator/index.md)
- [`yii\web\AssetConverter`](/docs/api/yii/web/assetconverter/index.md)
- [`yii\web\AssetManager`](/docs/api/yii/web/assetmanager/index.md)
- [`yii\web\HtmlResponseFormatter`](/docs/api/yii/web/htmlresponseformatter/index.md)
- [`yii\web\JsonResponseFormatter`](/docs/api/yii/web/jsonresponseformatter/index.md)
- [`yii\web\Session`](/docs/api/yii/web/session/index.md)
- [`yii\web\UrlManager`](/docs/api/yii/web/urlmanager/index.md)
- [`yii\web\User`](/docs/api/yii/web/user/index.md)
- [`yii\web\XmlResponseFormatter`](/docs/api/yii/web/xmlresponseformatter/index.md)
- [`yii\widgets\ActiveField`](/docs/api/yii/widgets/activefield/index.md)
- [`yii\debug\Panel`](/docs/extensions/debug/reference/panel/index.md)
- [`yii\inertia\Manager`](/docs/extensions/inertia/reference/manager/index.md)

Component is the base class that implements the *property*, *event* and *behavior* features.

Component provides the *event* and *behavior* features, in addition to the *property* feature which is implemented in
its parent class [BaseObject](/docs/api/yii/base/baseobject/index.md).

Event is a way to "inject" custom code into existing code at certain places. For example, a comment object can trigger
an "add" event when the user adds a comment. We can write custom code and attach it to this event so that when the event
is triggered (i.e. comment will be added), our custom code will be executed.

An event is identified by a name that should be unique within the class it is defined at. Event names are *case-sensitive*.

One or multiple PHP callbacks, called *event handlers*, can be attached to an event. You can call [`trigger()`](/docs/api/yii/base/component/index.md#method-trigger) to
raise an event. When an event is raised, the event handlers will be invoked automatically in the order they were
attached.

To attach an event handler to an event, call [`on()`](/docs/api/yii/base/component/index.md#method-on):

```
$post->on('update', function ($event) {
    // send email notification
});
```

In the above, an anonymous function is attached to the "update" event of the post. You may attach
the following types of event handlers:

- anonymous function: `function ($event) { ... }`
- object method: `[$object, 'handleAdd']`
- static class method: `['Page', 'handleAdd']`
- global function: `'handleAdd'`

The signature of an event handler should be like the following:

```
function foo($event)
```

where `$event` is an [`Event`](/docs/api/yii/base/event/index.md) object which includes parameters associated with the event.

You can also attach a handler to an event when configuring a component with a configuration array.
The syntax is like the following:

```
[
    'on add' => function ($event) { ... }
]
```

where `on add` stands for attaching an event to the `add` event.

Sometimes, you may want to associate extra data with an event handler when you attach it to an event
and then access it when the handler is invoked. You may do so by

```
$post->on('update', function ($event) {
    // the data can be accessed via $event->data
}, $data);
```

A behavior is an instance of [`Behavior`](/docs/api/yii/base/behavior/index.md) or its child class. A component can be attached with one or multiple
behaviors. When a behavior is attached to a component, its public properties and methods can be accessed via the
component directly, as if the component owns those properties and methods.

To attach a behavior to a component, declare it in [`behaviors()`](/docs/api/yii/base/component/index.md#method-behaviors), or explicitly call [`attachBehavior()`](/docs/api/yii/base/component/index.md#method-attachBehavior). Behaviors
declared in [`behaviors()`](/docs/api/yii/base/component/index.md#method-behaviors) are automatically attached to the corresponding component.

One can also attach a behavior to a component when configuring it with a configuration array. The syntax is like the
following:

```
[
    'as tree' => [
        'class' => 'Tree',
    ],
]
```

where `as tree` stands for attaching a behavior named `tree`, and the array will be passed to [`Yii::createObject()`](/docs/api/yii/baseyii/index.md#method-createObject)
to create the behavior object.

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

## Properties

| Property | Type | Description | Defined by |
| --- | --- | --- | --- |
| <span id="property-behaviors"></span>`$behaviors` read-only | `array<`[`Behavior`](/docs/api/yii/base/behavior/index.md)`<static>>` | List of behaviors attached to this component. | `Component` |

## 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) |
| [`__call()`](#method-__call) | Calls the named method which is not a class method. | `Component` |
| [`__clone()`](#method-__clone) | This method is called after the object is created by cloning an existing one. | `Component` |
| [`__get()`](#method-__get) | Returns the value of a component property. | `Component` |
| [`__isset()`](#method-__isset) | Checks if a property is set, i.e. defined and not null. | `Component` |
| [`__set()`](#method-__set) | Sets the value of a component property. | `Component` |
| [`__unset()`](#method-__unset) | Sets a component property to be null. | `Component` |
| [`attachBehavior()`](#method-attachBehavior) | Attaches a behavior to this component. | `Component` |
| [`attachBehaviors()`](#method-attachBehaviors) | Attaches a list of behaviors to the component. | `Component` |
| [`behaviors()`](#method-behaviors) | Returns a list of behaviors that this component should behave as. | `Component` |
| [`canGetProperty()`](#method-canGetProperty) | Returns a value indicating whether a property can be read. | `Component` |
| [`canSetProperty()`](#method-canSetProperty) | Returns a value indicating whether a property can be set. | `Component` |
| <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) |
| [`detachBehavior()`](#method-detachBehavior) | Detaches a behavior from the component. | `Component` |
| [`detachBehaviors()`](#method-detachBehaviors) | Detaches all behaviors from the component. | `Component` |
| [`ensureBehaviors()`](#method-ensureBehaviors) | Makes sure that the behaviors declared in [`behaviors()`](/docs/api/yii/base/component/index.md#method-behaviors) are attached to this component. | `Component` |
| [`getBehavior()`](#method-getBehavior) | Returns the named behavior object. | `Component` |
| [`getBehaviors()`](#method-getBehaviors) | Returns all behaviors attached to this component. | `Component` |
| [`hasEventHandlers()`](#method-hasEventHandlers) | Returns a value indicating whether there is any handler attached to the named event. | `Component` |
| [`hasMethod()`](#method-hasMethod) | Returns a value indicating whether a method is defined. | `Component` |
| [`hasProperty()`](#method-hasProperty) | Returns a value indicating whether a property is defined for this component. | `Component` |
| <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) | Detaches an existing event handler from this component. | `Component` |
| [`on()`](#method-on) | Attaches an event handler to an event. | `Component` |
| [`trigger()`](#method-trigger) | Triggers an event. | `Component` |

## Method details

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

```php
public function __call(string $name, array $params): mixed
```

Calls the named method which is not a class method.

This method will check if any attached behavior has
the named method and will execute it if available.

Do not call this method directly as it is a PHP magic method that
will be implicitly called when an unknown method is being invoked.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$name` | `string` | The method name |
| `$params` | `array` | Method parameters |

**Returns**

`mixed`: The method return value

**Throws**

- [`UnknownMethodException`](/docs/api/yii/base/unknownmethodexception/index.md): when calling unknown method

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

```php
public function __clone(): mixed
```

This method is called after the object is created by cloning an existing one.

It removes all behaviors because they are attached to the old object.

**Returns**

`mixed`

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

```php
public function __get(string $name): mixed
```

Returns the value of a component property.

This method will check in the following order and act accordingly:

 - a property defined by a getter: return the getter result
 - a property of a behavior: return the behavior property value

Do not call this method directly as it is a PHP magic method that
will be implicitly called when executing `$value = $component->property;`.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$name` | `string` | The property name |

**Returns**

`mixed`: The property value or the value of a behavior's property

**Throws**

- [`UnknownPropertyException`](/docs/api/yii/base/unknownpropertyexception/index.md): if the property is not defined
- [`InvalidCallException`](/docs/api/yii/base/invalidcallexception/index.md): if the property is write-only.

**See also**

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

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

```php
public function __isset(string $name): bool
```

Checks if a property is set, i.e. defined and not null.

This method will check in the following order and act accordingly:

 - a property defined by a setter: return whether the property is set
 - a property of a behavior: return whether the property is set
 - return `false` for non existing properties

Do not call this method directly as it is a PHP magic method that
will be implicitly called when executing `isset($component->property)`.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$name` | `string` | The property name or the event name |

**Returns**

`bool`: Whether the named property is set

**See also**

- <https://www.php.net/manual/en/function.isset.php>

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

```php
public function __set(string $name, mixed $value): mixed
```

Sets the value of a component property.

This method will check in the following order and act accordingly:

 - a property defined by a setter: set the property value
 - an event in the format of "on xyz": attach the handler to the event "xyz"
 - a behavior in the format of "as xyz": attach the behavior named as "xyz"
 - a property of a behavior: set the behavior property value

Do not call this method directly as it is a PHP magic method that
will be implicitly called when executing `$component->property = $value;`.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$name` | `string` | The property name or the event name |
| `$value` | `mixed` | The property value |

**Returns**

`mixed`

**Throws**

- [`UnknownPropertyException`](/docs/api/yii/base/unknownpropertyexception/index.md): if the property is not defined
- [`InvalidCallException`](/docs/api/yii/base/invalidcallexception/index.md): if the property is read-only.

**See also**

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

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

```php
public function __unset(string $name): mixed
```

Sets a component property to be null.

This method will check in the following order and act accordingly:

 - a property defined by a setter: set the property value to be null
 - a property of a behavior: set the property value to be null

Do not call this method directly as it is a PHP magic method that
will be implicitly called when executing `unset($component->property)`.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$name` | `string` | The property name |

**Returns**

`mixed`

**Throws**

- [`InvalidCallException`](/docs/api/yii/base/invalidcallexception/index.md): if the property is read only.

**See also**

- <https://www.php.net/manual/en/function.unset.php>

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

```php
public function attachBehavior(string $name, string|array|yii\base\Behavior $behavior): yii\base\Behavior<static>
```

Attaches a behavior to this component.

This method will create the behavior object based on the given
configuration. After that, the behavior object will be attached to
this component by calling the [`Behavior::attach()`](/docs/api/yii/base/behavior/index.md#method-attach) method.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$name` | `string` | The name of the behavior. |
| `$behavior` | `string\|array\|`[`Behavior`](/docs/api/yii/base/behavior/index.md) | The behavior configuration. This can be one of the following: - a [`Behavior`](/docs/api/yii/base/behavior/index.md) object - a string specifying the behavior class - an object configuration array that will be passed to [`Yii::createObject()`](/docs/api/yii/baseyii/index.md#method-createObject) to create the behavior object. |

**Returns**

[`Behavior`](/docs/api/yii/base/behavior/index.md)`<static>`: The behavior object

**See also**

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

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

```php
public function attachBehaviors(array $behaviors): mixed
```

Attaches a list of behaviors to the component.

Each behavior is indexed by its name and should be a [`Behavior`](/docs/api/yii/base/behavior/index.md) object,
a string specifying the behavior class, or an configuration array for creating the behavior.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$behaviors` | `array` | List of behaviors to be attached to the component |

**Returns**

`mixed`

**See also**

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

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

```php
public function behaviors(): array<array-key, class-string|array{class: class-string}|yii\base\Behavior>
```

Returns a list of behaviors that this component should behave as.

Child classes may override this method to specify the behaviors they want to behave as.

The return value of this method should be an array of behavior objects or configurations
indexed by behavior names. A behavior configuration can be either a string specifying
the behavior class or an array of the following structure:

```
'behaviorName' => [
    'class' => 'BehaviorClass',
    'property1' => 'value1',
    'property2' => 'value2',
]
```

Note that a behavior class must extend from [`Behavior`](/docs/api/yii/base/behavior/index.md). Behaviors can be attached using a name or anonymously.
When a name is used as the array key, using this name, the behavior can later be retrieved using [`getBehavior()`](/docs/api/yii/base/component/index.md#method-getBehavior)
or be detached using [`detachBehavior()`](/docs/api/yii/base/component/index.md#method-detachBehavior). Anonymous behaviors can not be retrieved or detached.

Behaviors declared in this method will be attached to the component automatically (on demand).

**Returns**

`array<array-key, class-string|array{class: class-string}|`[`Behavior`](/docs/api/yii/base/behavior/index.md)`>`: The behavior configurations.

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

```php
public function canGetProperty(string $name, bool $checkVars = true, bool $checkBehaviors = true): bool
```

Returns a value indicating whether a property can be read.

A property can be read if:

- the class has a getter method associated with the specified name
  (in this case, property name is case-insensitive);
- the class has a member variable with the specified name (when `$checkVars` is true);
- an attached behavior has a readable property of the given name (when `$checkBehaviors` is true).

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$name` | `string` | The property name |
| `$checkVars` | `bool` | Whether to treat member variables as properties |
| `$checkBehaviors` | `bool` | Whether to treat behaviors' properties as properties of this component |

**Returns**

`bool`: Whether the property can be read

**See also**

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

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

```php
public function canSetProperty(string $name, bool $checkVars = true, bool $checkBehaviors = true): bool
```

Returns a value indicating whether a property can be set.

A property can be written if:

- the class has a setter method associated with the specified name
  (in this case, property name is case-insensitive);
- the class has a member variable with the specified name (when `$checkVars` is true);
- an attached behavior has a writable property of the given name (when `$checkBehaviors` is true).

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$name` | `string` | The property name |
| `$checkVars` | `bool` | Whether to treat member variables as properties |
| `$checkBehaviors` | `bool` | Whether to treat behaviors' properties as properties of this component |

**Returns**

`bool`: Whether the property can be written

**See also**

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

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

```php
public function detachBehavior(string $name): yii\base\Behavior<static>|null
```

Detaches a behavior from the component.

The behavior's [`Behavior::detach()`](/docs/api/yii/base/behavior/index.md#method-detach) method will be invoked.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$name` | `string` | The behavior's name. |

**Returns**

[`Behavior`](/docs/api/yii/base/behavior/index.md)`<static>|null`: The detached behavior. Null if the behavior does not exist.

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

```php
public function detachBehaviors(): mixed
```

Detaches all behaviors from the component.

**Returns**

`mixed`

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

```php
public function ensureBehaviors(): mixed
```

Makes sure that the behaviors declared in [`behaviors()`](/docs/api/yii/base/component/index.md#method-behaviors) are attached to this component.

**Returns**

`mixed`

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

```php
public function getBehavior(string $name): yii\base\Behavior<static>|null
```

Returns the named behavior object.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$name` | `string` | The behavior name |

**Returns**

[`Behavior`](/docs/api/yii/base/behavior/index.md)`<static>|null`: The behavior object, or null if the behavior does not exist

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

```php
public function getBehaviors(): array<yii\base\Behavior<static>>
```

Returns all behaviors attached to this component.

**Returns**

`array<`[`Behavior`](/docs/api/yii/base/behavior/index.md)`<static>>`: List of behaviors attached to this component

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

```php
public function hasEventHandlers(string $name): bool
```

Returns a value indicating whether there is any handler attached to the named event.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$name` | `string` | The event name |

**Returns**

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

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

```php
public function hasMethod(string $name, bool $checkBehaviors = true): bool
```

Returns a value indicating whether a method is defined.

A method is defined if:

- the class has a method with the specified name
- an attached behavior has a method with the given name (when `$checkBehaviors` is true).

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$name` | `string` | The property name |
| `$checkBehaviors` | `bool` | Whether to treat behaviors' methods as methods of this component |

**Returns**

`bool`: Whether the method is defined

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

```php
public function hasProperty(string $name, bool $checkVars = true, bool $checkBehaviors = true): bool
```

Returns a value indicating whether a property is defined for this component.

A property is defined if:

- the class has a getter or setter method associated with the specified name
  (in this case, property name is case-insensitive);
- the class has a member variable with the specified name (when `$checkVars` is true);
- an attached behavior has a property of the given name (when `$checkBehaviors` is true).

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$name` | `string` | The property name |
| `$checkVars` | `bool` | Whether to treat member variables as properties |
| `$checkBehaviors` | `bool` | Whether to treat behaviors' properties as properties of this component |

**Returns**

`bool`: Whether the property is defined

**See also**

- [`canGetProperty()`](/docs/api/yii/base/component/index.md#method-canGetProperty)
- [`canSetProperty()`](/docs/api/yii/base/component/index.md#method-canSetProperty)

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

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

Detaches an existing event handler from this component.

This method is the opposite of [`on()`](/docs/api/yii/base/component/index.md#method-on).

Note: in case wildcard pattern is passed for 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 |
| --- | --- | --- |
| `$name` | `string` | 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`: If a handler is found and detached

**See also**

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

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

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

Attaches an event handler to an event.

The event handler must be a valid PHP callback. The following are
some examples:

```
function ($event) { ... }         // anonymous function
[$object, 'handleClick']          // $object->handleClick()
['Page', 'handleClick']           // Page::handleClick()
'handleClick'                     // global function handleClick()
```

The event handler must be defined with the following signature,

```
function ($event)
```

where `$event` is an [`Event`](/docs/api/yii/base/event/index.md) object which includes parameters associated with the event.

Since 2.0.14 you can specify event name as a wildcard pattern:

```
$component->on('event.group.*', function ($event) {
    Yii::trace($event->name . ' is triggered.');
});
```

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$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/component/index.md#method-off)

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

```php
public function trigger(string $name, yii\base\Event|null $event = null): mixed
```

Triggers an event.

This method represents the happening of an event. It invokes all attached handlers for the event
including class-level handlers.

**Parameters**

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

**Returns**

`mixed`