Let's say I have this JS code:
var events = ['start', 'stop', 'tick']
, obj = ... // object with events
, receiver = function(event_name, event_args){ // function to receive all events
console.log(event_name, event_args);
}
;
for(var i=0; i<events.length; i++){
obj.on(events[i], (function(event){
return function(){
return receiver(event, arguments);
};
})(events[i]));
}
What I'm doing here is to route all events to function "receiver".
I'm trying to replicate this behavior in PHP, since I already have obj.on
($obj->on
) functionality.
I would like to support (slightly) old PHP versions (5.1+), so I can't make use of anonymous functions (as well as passing variables to said functions).
Here's a tentative (and incomplete) version of the above code:
$events = array('start', 'stop', 'tick');
$obj = new ... ; // object with events
function receiver($event_name, $event_args){ // function to receive all events
echo $event_name.' | '.print_r($event_args, true);
}
foreach($events as $event){
$obj->on($event, ...); // ???
// ???
return receiver($event, func_get_arguments());
// ???
}