php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #50037 No is_function() to check whether variable is an anonymous function
Submitted: 2009-10-29 21:24 UTC Modified: 2009-10-30 16:02 UTC
From: mjs at beebo dot org Assigned:
Status: Not a bug Package: Feature/Change Request
PHP Version: 5.3.0 OS: OS X
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
13 + 23 = ?
Subscribe to this entry?

 
 [2009-10-29 21:24 UTC] mjs at beebo dot org
Description:
------------
There doesn't seem to be any good way to check whether a variable is a 
closure/anonymous function.  If

$fn = function() { };

gettype($fn) is "object", and there is no is_function() or equivalent.  
The only way seems to be

gettype($fn) == "object" && get_class($fn) == "Closure"

but the anonymous function documentation says that the fact that 
anonymous functions have class Closure "is an implementation detail 
and should not be relied upon."

I suggest:

1. gettype() returns "function" or "closure" when passed a closure.

2. is_function() is created that returns true when passed a closure, 
otherwise false.


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-10-30 16:02 UTC] johannes@php.net
Sorry, but your problem does not imply a bug in PHP itself.  For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system is not the
appropriate forum for asking support questions.  Due to the volume
of reports we can not explain in detail here why your report is not
a bug.  The support channels will be able to provide an explanation
for you.

Thank you for your interest in PHP.

Use is_callable()
 [2010-11-08 23:49 UTC] hn dot campbell at gmail dot com
Worth nothing here: as far as I can see there is no way to tell the difference 
between a Closure and a Functor (any class with an __invoke method) of some 
kind... both are objects, both are callable. 

The only detectable difference I can see is that you can't create a 
ReflectionFunction from a class - not a very useful test.
 
PHP Copyright © 2001-2022 The PHP Group
All rights reserved.
Last updated: Tue May 24 09:05:44 2022 UTC