# Behavior

> Behavior is the base class for all behavior classes.

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

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

7 subclasses:

- [`ActionFilter`](/docs/api/yii/base/actionfilter/index.md)
- [`yii\behaviors\AttributeBehavior`](/docs/api/yii/behaviors/attributebehavior/index.md)
- [`yii\behaviors\AttributeTypecastBehavior`](/docs/api/yii/behaviors/attributetypecastbehavior/index.md)
- [`yii\behaviors\AttributesBehavior`](/docs/api/yii/behaviors/attributesbehavior/index.md)
- [`yii\behaviors\CacheableWidgetBehavior`](/docs/api/yii/behaviors/cacheablewidgetbehavior/index.md)
- [`yii\db\mssql\RowVersionBehavior`](/docs/api/yii/db/mssql/rowversionbehavior/index.md)
- [`yii\filters\VerbFilter`](/docs/api/yii/filters/verbfilter/index.md)

Behavior is the base class for all behavior classes.

A behavior can be used to enhance the functionality of an existing component without modifying its code.
In particular, it can "inject" its own methods and properties into the component
and make them directly accessible via the component. It can also respond to the events triggered in the component
and thus intercept the normal code execution.

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

## Properties

| Property | Type | Description | Defined by |
| --- | --- | --- | --- |
| <span id="property-owner"></span>`$owner` | `yii\base\T\|null` | The owner of this behavior Default: `null`. | `Behavior` |

## 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) |
| [`attach()`](#method-attach) | Attaches the behavior object to the component. | `Behavior` |
| <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) |
| [`detach()`](#method-detach) | Detaches the behavior object from the component. | `Behavior` |
| [`events()`](#method-events) | Declares event handlers for the [`$owner`](/docs/api/yii/base/behavior/index.md#property-owner)'s events. | `Behavior` |
| <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) |

## Method details

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

```php
public function attach(yii\base\T $owner): mixed
```

Attaches the behavior object to the component.

The default implementation will set the [`$owner`](/docs/api/yii/base/behavior/index.md#property-owner) property
and attach event handlers as declared in [`events()`](/docs/api/yii/base/behavior/index.md#method-events).
Make sure you call the parent implementation if you override this method.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$owner` | `yii\base\T` | The component that this behavior is to be attached to. |

**Returns**

`mixed`

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

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

Detaches the behavior object from the component.

The default implementation will unset the [`$owner`](/docs/api/yii/base/behavior/index.md#property-owner) property
and detach event handlers declared in [`events()`](/docs/api/yii/base/behavior/index.md#method-events).
Make sure you call the parent implementation if you override this method.

**Returns**

`mixed`

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

```php
public function events(): array
```

Declares event handlers for the [`$owner`](/docs/api/yii/base/behavior/index.md#property-owner)'s events.

Child classes may override this method to declare what PHP callbacks should
be attached to the events of the [`$owner`](/docs/api/yii/base/behavior/index.md#property-owner) component.

The callbacks will be attached to the [`$owner`](/docs/api/yii/base/behavior/index.md#property-owner)'s events when the behavior is
attached to the owner; and they will be detached from the events when
the behavior is detached from the component.

The callbacks can be any of the following:

- method in this behavior: `'handleClick'`, equivalent to `[$this, 'handleClick']`
- object method: `[$object, 'handleClick']`
- static method: `['Page', 'handleClick']`
- anonymous function: `function ($event) { ... }`

The following is an example:

```
[
    Model::EVENT_BEFORE_VALIDATE => 'myBeforeValidate',
    Model::EVENT_AFTER_VALIDATE => 'myAfterValidate',
]
```

**Returns**

`array`: Events (array keys) and the corresponding event handler methods (array values).