A behavior can be attached to any class that extends from `Component`. In order to attach a behavior to a class, the component class must implement the `behaviors`
method. As an example, Yii provides the `AutoTimestamp` behavior for automatically updating timestamp fields when saving an Active Record model:
In the above, the `class` value is a string representing the fully qualified behavior class name. All of the other key-value pairs represent corresponding public properties of the `AutoTimestamp` class, thereby customizing how the behavior functions.
To create your own behavior, you must define a class that implements the `IBehavior` interface. This can be accomplished by extending `CBehavior`. More specifically, you can extend `CModelBehavior` or `CActiveRecordBehavior` for behaviors to be used specifically with models or with Active Record models.
```php
class MyBehavior extends CActiveRecordBehavior
{
}
```
To make your behavior customizable, like `AutoTimestamp`, add public properties:
```php
class MyBehavior extends CActiveRecordBehavior
{
public $attr;
}
```
Now, when the behavior is used, you can set the attribute to which you'd want the behavior to be applied:
```php
class User extends ActiveRecord
{
// ...
public function behaviors()
{
return [
'mybehavior' => [
'class' => 'ext\mybehavior\MyBehavior',
'attr' => 'member_type'
],
],
];
}
}
```
Behaviors are normally written to take action when certain model-related events occur, such as `beforeSave` or `afterFind`. You can write your behaviors to have the corresponding method. Within the method, you can access the model instance through `$this->getOwner()`: