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
)
*/