For reasons I won’t bother you with, I need to distinguish between callable types. By that I mean, I need to know if a callable is either a global function, a closure, an object containing the magic method __invoke and so on.
I don’t want to test for the “Closure” class name. PHP manual says the fact that anonymous functions are implemented using the Closure class it’s an implementation detail and should not be relied upon. My best bet to determine if a callable is a closure without testing for the “Closure” class name is through elimination. The code in my first post works, I was just wondering if there are other (perhaps more elegant) alternatives (thanks for your Reflection idea though, I may be able to use it).
isn’t the whole point of closures and magic methods to work like a function? Imho, if you need to work this out the problem is not “how do I work it out”.
No, that leaves 3 other options: a closure, an instance method and a class method, unless function_exists works on instance and class methods besides global functions. Is callable returns true on all of the following:
class A {
public function test() {}
public static function staticTest() {}
public function __invoke() {}
}
function func() {}
$lambda = function() {};
$a = new A();
is_callable( array( $a, 'test' ) ); // instance method
is_callable( array( 'A', 'staticTest' ) ); // static method
is_callable( $a ); // because of __invoke
is_callable( 'func' ); // global function
is_callable( $lambda ); // anonymous function
Seems like reinventing the wheel to me, but just looking back at your function if is_callable returns true, and it’s not an object or a declared function (use function_exists) through process of elimination it must be a closure mustn’t it?
Use Reflection. ReflectionClass/Object first, this will catch __invoke and Closures. (Closures are classes with a classname of “{Closure}” if I remember correclty.} The can use ReflectionFunction.