9

The PHP manual for anonymous functions (ie, Closures) states that:

Anonymous functions are currently implemented using the Closure class. This is an implementation detail and should not be relied upon.

(Emphasis is my own)

Is it possible to test a variable, such that the test returns true only if the variable is a Closure, without referring to the Closure class?

In other words, how can I rewrite the following such that it will raise an error when $bar is anything but an anonymous function:

function foo(Closure $bar) {
    $bar();
}

EDIT: Based on the answers received, here is an example test.

Notes:

  1. It seems there is no way to differentiate between Functors and Closures, and that the test is probably just as 'implementation specific' as using the Closure class.
  2. The (seemingly obvious) ReflectionFunction::isClosure() method seems to be be almost useless: by the time you've done the checks required to make sure that ReflectionFunction can actually be instantiated (can't take a Class except for a Closure), you've eliminated all other options.
  3. In 5.3.0 you ReflectionClass($closure)->hasMethod('__invoke') returned false, so this could be used as a test against Functors, however (I'm told) this has changed since. This highlights the frailty of the solution too.
  4. Follow up from Gordon - As of PHP 5.4 you can rely on Closure being a Closure: php.net/manual/en/class.closure.php

Code:

/**
 * Return true if and only if the passed argument is a Closure.
 */
function testClosure($a) {
    // Must be Callback, Labmda, Functor or Closure:
    if(!is_callable($a)) return false;

    // Elminate Callbacks & Lambdas
    if(!is_object($a)) return false;

    // Eliminate Functors
    //$r = new ReflectionFunction($a); <-- fails if $a is a Functor
    //if($r->isClosure()) return true;

    return false;
}

Test case:

//////////// TEST CASE /////////////

class CallBackClass {
    function callBackFunc() {
    }
}

class Functor {
    function __invoke() {
    }
}

$functor = new Functor();
$lambda = create_function('', '');
$callback = array('CallBackClass', 'callBackFunc');
$array = array();
$object = new stdClass();
$closure = function() { ; };

echo "Is it a closure? \n";
echo "Closure: " . (testClosure($closure) ? "yes" : "no") . "\n";
echo "Null: "  . (testClosure(null) ? "yes" : "no") . "\n";
echo "Array: " . (testClosure($array) ? "yes" : "no") . "\n";
echo "Callback: " . (testClosure($callback) ? "yes" : "no")  . "\n";
echo "Labmda: " .(testClosure($lambda) ? "yes" : "no") . "\n";
echo "Invoked Class: " . (testClosure($functor) ? "yes" : "no")  . "\n";
echo "StdObj: " . (testClosure($object) ? "yes" : "no") . "\n";

-

5
  • 1
    What about functions created using create_function, the poor man's anonymous functions from before PHP 5.3? What about objects that can be __invoke()d?
    – janmoesen
    Nov 8, 2010 at 20:57
  • I'm specifically interested in whether you can test for a Closure ("...returns true only if the variable is a Closure...")
    – Hamish
    Nov 8, 2010 at 21:11
  • 2
    My question to you is why would you want to distinguish between a functor (or any callback at that point) and a closure? In 5.3, there's no difference from your point of view (since you can't rebind anything), so what does it matter? If it passes is_callable(), then why isn't that enough???
    – ircmaxell
    Sep 16, 2011 at 14:37
  • As of PHP 5.4 you can rely on Closure being a Closure: php.net/manual/en/class.closure.php
    – Gordon
    Jan 26, 2012 at 13:47
  • 1
    @Gordon thanks for the follow up! I'll add it above.
    – Hamish
    Jan 26, 2012 at 20:04

2 Answers 2

8

You can also use

ReflectionFunctionAbstract::isClosure — Checks if closure

Example:

$poorMansLambda = create_function('', 'return TRUE;');
$rf = new ReflectionFunction($poorMansLambda);
var_dump( $rf->isClosure() ); // FALSE

$lambda  = function() { return TRUE; };   
$rf = new ReflectionFunction($lambda);
var_dump( $rf->isClosure() ); // TRUE

$closure = function() use ($lambda) { return $lambda(); };    
$rf = new ReflectionFunction($closure);
var_dump( $rf->isClosure() ); // TRUE

Note that the above will only return TRUE for PHP 5.3 Lambdas and Closures. If you just want to know whether an argument can be used as a callback, is_callable will perform better.


EDIT If you want to include Functors as well, you can do (as of PHP 5.3.3)

$rf = new ReflectionObject($functorOrClosureOrLambda);
var_dump( $rf->hasMethod('__invoke') ); // TRUE

or

method_exists($functorOrClosureOrLambda, '__invoke');

with the latter being the faster alternative.

A Closure instance is basically just a class that has an __invoke function which you fed the method body on the fly. But since this is testing for an implementation detail, I'd say it is as unreliable as testing for the Closure Class Name.


EDIT Since you mention you cannot reliably test via the Reflection API due to it raising an error when passing a Functor to ReflectionFunctionAbstract::isClosure, try if the following solution suits your needs:

function isClosure($arg)
{
    if(is_callable($arg, FALSE, $name)) {
        is_callable(function() {}, TRUE, $implementation);
        return ($name === $implementation);
    }
}

This will check if the passed argument is callable. The $name argument stores the callable name. For closures, this is currently Closure::__invoke. Since this will be the same for any Closures/Lambdas, we can compare the name of the passed argument against an arbitrary other Closure/Lambda. If they are equal, the argument must be a Closure/Lambda. Determining the callable name at runtime has the added benefit that you dont have to hardcode assumptions about the implementation details into your sourcecode. Passing a functor will return FALSE, because it wont have the same callable name. Since this does not rely on the Reflection API, it is also likely a bit faster.

The above could be more elegantly written as

function isClosure($arg) {
    $test = function(){};
    return $arg instanceof $test;
}
18
  • 1
    Nice, I likes me some reflection.
    – janmoesen
    Nov 8, 2010 at 21:12
  • Nice. Will this approach get past his "implementation detail" problem? I guess I'm really asking if isClosure checks for class Closure.
    – webbiedave
    Nov 8, 2010 at 21:14
  • 2
    @webbiedave - that doesn't really matter, since I'd assume that an implementation change to Closures would be replaced in ReflectionFunction
    – Hamish
    Nov 8, 2010 at 21:17
  • @webbiedave it's an API call, so you dont have to bother about it's implementation details and can rely on the devs to update the Reflection API whenever they update the class used for Closures/Lambdas. But since I dont see any hardcoded classname in the sourcecode, I'd say it determines it somehow differently (ZEND_ACC_CLOSURE) anyway.
    – Gordon
    Nov 8, 2010 at 21:22
  • @Gordon - almost, but not quite. How do you test between a StdObj, an __invoke-able object and a Closure?
    – Hamish
    Nov 8, 2010 at 21:24
1

is_callable and !is_array might help you along. Note that you cannot rely on PHP's type hinting/checking this way, since you would have to check the variable inside the function and throw something, e.g. an InvalidArgumentException yourself.

2
  • +1 though it should be noted that this is_callable will return TRUE for any callables, not just Closures.
    – Gordon
    Nov 8, 2010 at 20:56
  • 1
    @Gordon: quite right, I was adding that as a comment to the original question while you posted this. :-)
    – janmoesen
    Nov 8, 2010 at 20:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.