I have an idea for an event system that I am developing for my own platform.
Imagine a pseudo-function like this.
class Test { public function hi() { Event::add(__FUNCTION__ . 'is about to run.'); return "hi"; } }
Imagine you need to do the same for some other functions. (Perhaps you want to log what functions were executed at run time, and want to write them to a separate file.)
Instead, and adding events to functions manually, can we do something like this?
class Test { public function hi() { return "hi"; } } // events.php (It a pseudo code so may not work.) // Imagine extend purpose is to inject codes into target function Event::bind('on', $className, $methodName, function() use ($className, $methodName) { return $className->$methodName->extend('before', Event::add(__FUNCTION__ . 'is about to run.')); });
The idea is to introduce the hi() function, which is inside the Test class and inject everything we pass into extend with the function outside. 'before' means that the injection should be on the first line of the target function.
Finally, events and event bindings are completely abstracted from functions. I want to be able to bind custom things without changing functions.
I have a feeling that we can do this by hacking eval() or by playing with call_user_func() . Although I'm not sure. Using eval() sounds pretty bad already.
My question is:
- Is it possible to do this with PHP?
- Does he have a name in OOP / OOP principles so that I can read further?
- Does it make sense or is it a bad idea?
Aristona
source share