Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3333760
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T23:49:54+00:00 2026-05-17T23:49:54+00:00

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

  • 0

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";

–

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T23:49:55+00:00Added an answer on May 17, 2026 at 11:49 pm

    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;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

According to the PHP manual , a class like this: abstract class Example {}
In the PHP manual, to show the syntax for functions with optional parameters, they
PHP manual suggests to autoload classes like function __autoload($class_name){ require_once(some_dir/.$class_name..php); } and this approach
This is from the php manual: http://us.php.net/manual/en/language.constants.syntax.php If you use an undefined constant, PHP
The example from the PHP manual is using OOP. Is there a way to
According to the PHP manual , in order to make code more portable, they
In the PHP manual, the file operations reference an optional context (e.g. for copy
I'm looking at the PHP Manual , and I'm not seeing a section on
What does the following code do? A link to something in the PHP manual
Or: Should I optimize my string-operations in PHP? I tried to ask PHP's manual

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.