title: Testing for a PHP Closure without referring to the Closure internal class
body: <p>The <a href="http://php.net/manual/en/functions.anonymous.php" rel="nofollow noreferrer">PHP manual for anonymous functions</a> (ie, Closures) states that:</p>
  
  <blockquote>
    <p>Anonymous functions are currently implemented using the Closure class. This is an implementation detail and <strong>should not be relied upon</strong>.</p>
  </blockquote>
  
  <p><em>(Emphasis is my own)</em></p>
  
  <p>Is it possible to test a variable, such that the test returns true only if the variable is a Closure, <strong>without referring to the Closure class</strong>?</p>
  
  <p>In other words, how can I rewrite the following such that it will raise an error when <code>$bar</code> is anything but an anonymous function:</p>
  
  <pre>function foo(Closure $bar) {
      $bar();
  }</pre>
  
  <p>EDIT: Based on the answers received, here is an example test. </p>
  
  <p>Notes:</p>
  
  <ol>
  <li>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. </li>
  <li>The (seemingly obvious) <code>ReflectionFunction::isClosure()</code> 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.</li>
  <li>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.</li>
  <li>Follow up from <a href="https://stackoverflow.com/users/208809/gordon">Gordon</a> - As of PHP 5.4 you can rely on Closure being a Closure: <a href="http://php.net/manual/en/class.closure.php" rel="nofollow noreferrer">php.net/manual/en/class.closure.php</a></li>
  </ol>
  
  <p>Code:</p>
  
  <pre><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 &amp; Lambdas
      if(!is_object($a)) return false;
  
      // Eliminate Functors
      //$r = new ReflectionFunction($a); &lt;-- fails if $a is a Functor
      //if($r-&gt;isClosure()) return true;
  
      return false;
  }
  </code></pre>
  
  <p>Test case:</p>
  
  <pre><code>//////////// 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";
  </code></pre>
  
  <p>-</p>