hi.. i am curious about executing any public function (in any class) by only it's name just like the php's call_user_func()
if possible, i wants the code to be generic. The question is , is it even possible ?
--------------------code------------------------------
package ;
/*
* Hook.hx - A wordpress-like hook implementation
*
* available for flash8, flash, neko, js, php, cpp, cs, java
*/
import Reflect;
class Hook
{
public static var hooks:Array<Array<String>>;
public function new()
{
hooks = [];
}
public static function registerHook(hookName:String, registerFunction:String)
{
return hooks.push([hookName, registerFunction]);
}
public static function applyHook(hookName:String)
{
var affectedHooks:Array<Array<String>> = getRegisterHook(hookName);
/*
* i can't understand how reflects works. Please enlight me
for (i in affectedHooks)
{
var a = new Main();
Reflect.callMethod(a, Reflect.field(a, i[1]), []);
}
*/
}
private static function getRegisterHook(hookName:String)
{
var temp_hook_list:Array<Array<String>> = [];
for (i in hooks)
{
if (i[0] == hookName)
{
temp_hook_list.push(i);
}
}
return temp_hook_list;
}
}
---------------------------usage----------------------------------------------
function new()
{
new Hook();
Hook.registerHook("test", "testfunc");
Hook.registerHook("test", "testfunc2");
trace(Hook.hooks);
Hook.applyHook("test");
}
public function testfunc()
{
trace("calling from testfunc");
}
public function testfunc2()
{
trace("calling from testfunc2");
}
------------------------------------------------------------------------