PHPackages                             yii2tech/behavior-trait - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. yii2tech/behavior-trait

AbandonedArchivedYii2-extension[Utility &amp; Helpers](/categories/utility)

yii2tech/behavior-trait
=======================

Allows handling events via inline declared methods, which can be added by traits

1.0.0(10y ago)20320.1k↓49.6%3BSD-3-ClausePHP

Since Dec 26Pushed 6y ago4 watchersCompare

[ Source](https://github.com/yii2tech/behavior-trait)[ Packagist](https://packagist.org/packages/yii2tech/behavior-trait)[ RSS](/packages/yii2tech-behavior-trait/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (1)Versions (2)Used By (0)

 [ ![](https://avatars2.githubusercontent.com/u/12951949) ](https://github.com/yii2tech)

Behavior Trait Extension for Yii 2
==================================

[](#behavior-trait-extension-for-yii-2)

This extension provides the ability of handling events via inline declared methods, which can be added via traits.

For license information check the [LICENSE](LICENSE.md)-file.

[![Latest Stable Version](https://camo.githubusercontent.com/4a7df7fcab660ee47ce686025964a57525f581b278957ccc2ce325714a474887/68747470733a2f2f706f7365722e707567782e6f72672f79696932746563682f6265686176696f722d74726169742f762f737461626c652e706e67)](https://packagist.org/packages/yii2tech/behavior-trait)[![Total Downloads](https://camo.githubusercontent.com/168f4bf994a6ac627f208e19b8b89eebb08bd48afd30d21f25c3957222f1c411/68747470733a2f2f706f7365722e707567782e6f72672f79696932746563682f6265686176696f722d74726169742f646f776e6c6f6164732e706e67)](https://packagist.org/packages/yii2tech/behavior-trait)[![Build Status](https://camo.githubusercontent.com/c08ae4192592823e527f4818547fc3e152d77404898d5cadb9f5fc66469534bd/68747470733a2f2f7472617669732d63692e6f72672f79696932746563682f6265686176696f722d74726169742e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/yii2tech/behavior-trait)

Installation
------------

[](#installation)

The preferred way to install this extension is through [composer](http://getcomposer.org/download/).

Either run

```
php composer.phar require --prefer-dist yii2tech/behavior-trait

```

or add

```
"yii2tech/behavior-trait": "*"
```

to the require section of your composer.json.

Usage
-----

[](#usage)

This extension introduces special trait \[\[\\yii2tech\\behaviortrait\\BehaviorTrait\]\], which if used provides the ability of handling events via inline declared methods, which can be added via other traits. This trait can be added to any descendant of \[\[\\yii\\base\\Component\]\].

Each event handler method should be named by pattern: '{eventName}Handler{UniqueSuffix}', where 'eventName' is a name of the event the method should handle, 'UniqueSuffix' any suffix, which separate particular event handler method from the others. For example: if the class has an event 'beforeInsert' it can introduce method named `beforeInsertHandlerEncryptPassword`, which will be automatically triggered when event 'beforeInsert' is triggered:

```
use yii\db\ActiveRecord;
use yii2tech\behaviortrait\BehaviorTrait;

class User extends ActiveRecord
{
    use BehaviorTrait; // add `BehaviorTrait` allowing to use inline event handlers
    use EncryptPasswordTrait; // add trait, which introduce inline event handler

    // ...
}

trait EncryptPasswordTrait
{
    public function beforeInsertHandlerEncryptPassword(Event $event)
    {
        if (!empty($this->newPassword)) {
            $this->password = sha1($this->newPassword);
        }
    }
}
```

> Attention: watch for the naming collisions, ensure any inline handler declared either in class or via trait has a unique name (with unique suffix)!

> Note: using traits instead behaviors improves performance but is less flexible. Keep in mind that such approach has been rejected at Yii2 core at [yiisoft/yii2#1041](https://github.com/yiisoft/yii2/pull/1041).

###  Health Score

39

—

LowBetter than 86% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity44

Moderate usage in the ecosystem

Community11

Small or concentrated contributor base

Maturity63

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

3797d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/854af1889dd7384243cff0bf0fde23463f76058b499b15c740f02e7033ec7ce6?d=identicon)[klimov-paul](/maintainers/klimov-paul)

---

Top Contributors

[![klimov-paul](https://avatars.githubusercontent.com/u/1482054?v=4)](https://github.com/klimov-paul "klimov-paul (8 commits)")

---

Tags

behaviortraityiiyii2yii2-extensioneventtraityii2Behavior

### Embed Badge

![Health badge](/badges/yii2tech-behavior-trait/health.svg)

```
[![Health](https://phpackages.com/badges/yii2tech-behavior-trait/health.svg)](https://phpackages.com/packages/yii2tech-behavior-trait)
```

###  Alternatives

[loveorigami/yii2-plugins-system

Plugins system as WP for Yii2

1143.0k1](/packages/loveorigami-yii2-plugins-system)[sjaakp/yii2-taggable

Manage tags of ActiveRecord in Yii2.

3030.6k](/packages/sjaakp-yii2-taggable)[mdmsoft/yii2-autonumber

Auto number extension for the Yii framework

1830.9k](/packages/mdmsoft-yii2-autonumber)[baibaratsky/yii2-serialized-attributes-behavior

Yii2 Serialized Attributes Behavior

1174.1k9](/packages/baibaratsky-yii2-serialized-attributes-behavior)[asinfotrack/yii2-audittrail

Yii2-audittrail is a behavior and a set of widgets to track all modifications performed on a model

1727.0k](/packages/asinfotrack-yii2-audittrail)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
