Posts

Showing posts with the label Callback

Function.prototype.notifier

There are way too many ways to stub functions or methods, but at the end of the day all we want to know is always the same: has that function been invoked ? has that function received the expected context ? which argument has been passed to that function ? what was the output of the function ? Update thanks to @bga_ hint about the output property in after notification, it made perfect sense The Concept For fun and no profit I have created a prototype which aim is to bring a DOM like interface to any sort of function or method in order to monitor its lifecycle: the "before" event, able to preventDefault() and avoid the original function call at all the "after" event, in order to understand if the function did those expected changes to the environment or to a generic input object, or simply to analyze the output of the previous call the "error" event, in case we want to be notified if something went wrong during function execution the "handlerer...

PHP - apply, call, and Callback class

The "news" is that while some developer is putting a lot of effort to emulate PHP functionalities with JavaScript, I would like to have JavaScript functionalities in PHP. Nested functions, closures, and injected scope, are only some of JS cool stuff that's currently missing PHP (hoping that during this summer of code someone will implement at least one of them). Today what I'm going to emulate is a partial implementation of apply and call Function.prototype methods, and this is the basic example: function apply($name, array $arguments = array()){ return call_user_func_array($name, $arguments); } function call(){ $arguments = func_get_args(); return call_user_func_array(array_shift($arguments), $arguments); } As is for JavaScript, the main difference between these functions is that apply accept an array of arguments while call accepts an arbitrary number of arguments. echo call('md5', 'hello world'); // 5eb63bbbe01eeed093cb22bb8f5acdc3 ec...