PHP Classes

File: README.md

Recommend this page to a friend!
  Classes of Smoren Freelight   PHP Event based Inheritance Model   README.md   Download  
File: README.md
Role: Example script
Content type: text/markdown
Description: Example script
Class: PHP Event based Inheritance Model
Register handlers to process events in a chain
Author: By
Last change:
Date: 1 year ago
Size: 3,928 bytes
 

Contents

Class file image Download

Event-based inheritance model

????????? ????????? ?????????? ??????: - ?? ?????? ??????? ????? ???????? ?????????????? ?????????? ????????????, ??????? ?????????? ? ???? ??????? ???????; - ??? ????????? ??????? ??????????? ?????? ??????? ?????????? ?? ?????, ?????? ?? ????? ?????? ? ??????????? ??????????? ? ????? ??? ?????????, ???? ??? ??????????.

????? ??????? ??????????? ?????? "??????????? ????????????", ?? ???? ?????? ????????? ?????????? ????? ????????? ?????????? ???? ???????? ???-?? ????, ?????? ????????? ??? ??? ???????? ? ????? ???????.

?????????

composer require smoren/event-based-inheritance-model

???????????? ???????? ??????:

<?php

use Smoren\EventBasedInheritanceModel\EventBus;
use Smoren\EventBasedInheritanceModel\Listener;

// ??????? ????
$bus = new EventBus([
    // ?????? ??????? onTest ? ????????? ?? ???? ??????????
    'onTest' => function(&$params, Listener $listener) {
        $params['a']++;
        echo "onTest (0): ".json_encode($params)."\n";
    },
]);

// ????????? ? ???? ???????????? ??????? onTest ??? ???? ??????????
$bus->addListener('onTest', function(&$params, Listener $listener) {
    $listener->handlePrevious($params); // ? ?????? ?????? ?????????? ?? ????????? ??????????
    $params['a']++;
    echo "onTest (1): ".json_encode($params)."\n";
});

// ????????? ? ???? ???????????? ??????? onTest ??? ???? ??????????
$bus->addListener('onTest', function(&$params, Listener $listener) {
    $listener->handlePrevious($params); // ? ?????? ?????? ?????????? ?? ????????? ??????????
    $params['a']++;
    echo "onTest (2): ".json_encode($params)."\n";
    return array_merge($params, ['b' => 0]);
});

// ?????? ??????? onTest1 ? ????????? ?? ???? ??????????
$bus->addListener('onMyTest', function(&$params, Listener $listener) {
    // ? ?????? ?????? ?????????? ?? ????????? ??????????, ?? ??????????? ???, ??????? ?????? ?? ????? ??????
    $listener->handlePrevious($params);
    $params->a++;
    echo "onMyTest (0): ".json_encode($params)."\n";
});
// ????????? ? ???? ???????????? ??????? onTest1 ??? ???? ??????????
$bus->addListener('onMyTest', function(&$params, Listener $listener) {
    $listener->handlePrevious($params);
    $params->a++;
    echo "onMyTest (1): ".json_encode($params)."\n";
    return $params;
});

// ?????????? ??????? onTest
$params = ['a' => 1];
$result = $bus->trigger('onTest', $params);
print_r($result); // ? ?????????? ??????? ??, ??? ?????? ??????? ? ????? ??????????
/*
onTest (0): {"a":2}
onTest (1): {"a":3}
onTest (2): {"a":4}
Array
(
    [a] => 4
    [b] => 0
)
*/

echo "\n";

// ?????????? ??????? onMyTest
$params = new \stdClass();
$params->a = 1;
$result = $bus->trigger('onMyTest', $params);
print_r($result); // ? ?????????? ??????? ??, ??? ?????? ??????? ? ????? ??????????
/*
onMyTest (0): {"a":2}
onMyTest (1): {"a":3}
stdClass Object
(
    [a] => 3
)
*/