PHPackages                             horat1us/yii2-static-behavior - 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. [Framework](/categories/framework)
4. /
5. horat1us/yii2-static-behavior

ActiveLibrary[Framework](/categories/framework)

horat1us/yii2-static-behavior
=============================

Static Behavior for Yii2

1.1.0(7y ago)01.4kMITPHPPHP &gt;=7.2

Since Mar 6Pushed 7y ago2 watchersCompare

[ Source](https://github.com/Horat1us/yii2-static-behavior)[ Packagist](https://packagist.org/packages/horat1us/yii2-static-behavior)[ RSS](/packages/horat1us-yii2-static-behavior/feed)WikiDiscussions master Synced today

READMEChangelog (3)Dependencies (3)Versions (4)Used By (0)

Yii2 Static Behavior
====================

[](#yii2-static-behavior)

[![Build Status](https://camo.githubusercontent.com/cb554586248465720e4926d034cce9516841dadeb9051decdebcf473e2a520d2/68747470733a2f2f7472617669732d63692e6f72672f486f7261743175732f796969322d7374617469632d6265686176696f722e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/Horat1us/yii2-static-behavior)[![codecov](https://camo.githubusercontent.com/6d38bcc5190f0d395b99ccc644e3e9e67a213989d82eac549f96efac92c9ed77/68747470733a2f2f636f6465636f762e696f2f67682f486f7261743175732f796969322d7374617469632d6265686176696f722f6272616e63682f6d61737465722f67726170682f62616467652e737667)](https://codecov.io/gh/Horat1us/yii2-static-behavior)

This package implements static behavior to allow you append behavior-like event handlers to external objects.

Usage example: You use package that contains ActiveRecord. If you need to handle some events of this record (beforeInsert, afterInsert etc.), this package definitely for you.

It uses static `yii\base\Event::on()` and `yii\base\Event::off()` under the hood.

The main advantage of using this static behavior:

- event handler class (**ItemInterface**) will be instantiated before event will be called, so dependencies will be lazy-loaded too.

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

[](#installation)

```
composer require horat1us/yii2-static-behavior
```

Structure
---------

[](#structure)

- [StaticBehavior](./src/StaticBehavior.php) - main class that deals with attaching and detaching handlers.
- [Bootstrap](./src/StaticBehavior/Bootstrap.php) - configurable application bootstrap class. It use **StaticBehavior** to attach handlers before application request and detach after request.
- [ItemInterface](./src/StaticBehavior/ItemInterface.php) - describes simple event handler (to be used in **StaticBehavior**).
- [Item](./src/StaticBehavior/Item.php) - abstract **ItemInterface** implementation. Allows to configure handlers (for example methods) for different events.
- [Bootstrap\\Behavior](src/StaticBehavior/Bootstrap/Behavior.php) - behavior to attach handlers before action and detach after controller/module action. Should be used if some handlers will be used only when module executed.

Usage
-----

[](#usage)

### StaticBehavior

[](#staticbehavior)

First, you need to implement [ItemInterface](./src/StaticBehavior/ItemInterface.php). Then, configure your **StaticBehavior** with implemented items.

*Note: you can use \\Closure instead of item classes (not recommended)*

```
