PHP code example of horat1us / yii2-static-behavior

1. Go to this page and download the library: Download horat1us/yii2-static-behavior library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

horat1us / yii2-static-behavior example snippets




namespace Example;

use yii\db;
use yii\base;
use Horat1us\Yii\StaticBehavior;

class ExternalRecord extends db\ActiveRecord
{
    // some implementation you cannot change
}

$staticBehavior = new StaticBehavior([
    'target' => ExternalRecord::class,
    'items' => [
        base\Model::EVENT_BEFORE_VALIDATE => function(base\Event $event) {
            // you can handle event in \Closure
        },
        base\Model::EVENT_AFTER_VALIDATE => [
            // custom item implementation
            'class' => StaticBehavior\Item::class,
        ],
        db\ActiveRecord::EVENT_INIT => [
            // or use interface to implement items
            'class' => StaticBehavior\ItemInterface::class,
        ],
    ],
]);

// To attach event handlers to class
$staticBehavior->attach();

// To detach attached event handlers from class
$staticBehavior->detach();



use Horat1us\Yii\StaticBehavior;

// config.php

return [
    'bootstrap' => [
        'class' => StaticBehavior\Bootstrap::class,
        'behaviors' => [
            // StaticBehavior references. See previous section for examples.
        ],
    ],
];