actualwave/callbacks

Family of callback wrappers with shared interface

0.0.4 2016-01-05 12:27 UTC

This package is not auto-updated.

Last update: 2024-11-23 19:16:32 UTC


README

##PHP-Callbacks Build Status Coverage Status Dependency Status

Latest Stable Version Total Downloads License

Family of Callback Wrappers allowing to store chains of delayed calls that can be started by event.

  • FunctionCallback - calls global function or static method.
  • MethodCallback - calls instance or static method.
  • OutputCallback - outputs all arguments in JSON format.
  • PropertyCallback - stores first argument as property value, works with static properties.
  • VariableCallback - stores first argument as variable value, works with static properties.
  • CallableQueue - calls stored callbacks passing previous result as argument, returns result of last callback.
  • CallableSequence - calls stored callbacks with same arguments, returns result of last callback.
$variable = 'value';
$callback = new \aw\callbacks\VariableCallback('variable');
$callback('new value');
echo $variable.PHP_EOL; // new value

function doEcho($param){
  echo 'My name is: '.$param.PHP_EOL;
}

$callback = new \aw\callbacks\FunctionCallback('doEcho');
$callback('####'); // My name is: ####

Installation

Via composer

composer require actualwave/callbacks

Usage

All wrappers are callables, so can be used directly as closure. CallableCollection accepts any callable including PHP closures.

function multiply4($value){
  return $value*4;
}

$collection = new \aw\CallableQueue();
$collection[] = function($value){
  return $value*2;
};
$collection[] = function($value){
  return $value*3;
};
$collection[] = new \aw\callbacks\FunctionCallback('multiply4');
echo 'Result: '.$collection(2).PHP_EOL; // Result: 48