Update: The approach given here has a big drawback: the new function is not a fully qualified member of the class; $this not in the method when calling this method. This means that you need to pass the object to the function as a parameter if you want to work with data or functions from an object instance! In addition, you will not be able to access private or protected class members from these functions.
Good question and smart idea using new anonymous features!
Interestingly, this works: Replace
$me->doSomething();
by call_user_func on the function itself:
call_user_func($me->doSomething);
what doesn't work is the βrightβ way:
call_user_func(array($me, "doSomething"));
if called this way, PHP requires the method to be declared in the class definition.
Is this a private / public / protected visibility issue?
Update: None. It is not possible to call a function in the usual way, even from within the class, so this is not a visibility problem. Passing the actual function to call_user_func() is the only way I can do this work.
Pekka μ May 30, '10 at 9:00 a.m. 2010-05-30 09:00
source share