php is_function() 判断变量是否为函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2835627/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 07:49:29  来源:igfitidea点击:

php is_function() to determine if a variable is a function

phpanonymous-function

提问by Jage

I was pretty excited to read about anonymous functionsin php, which let you declare a variable that is function easier than you could do with create_function. Now I am wondering if I have a function that is passed a variable, how can I check it to determine if it is a function? There is no is_function() function yet, and when I do a var_dump of a variable that is a function::

我很高兴看到php 中的匿名函数,它可以让你声明一个比create_function更容易实现的变量。现在我想知道我是否有一个传递变量的函数,我如何检查它以确定它是否是一个函数?还没有 is_function() 函数,当我对作为函数的变量进行 var_dump 时:

$func = function(){
    echo 'asdf';
};
var_dump($func);

I get this:

我明白了:

object(Closure)#8 (0) { } 

Any thoughts on how to check if this is a function?

关于如何检查这是否是一个函数的任何想法?

回答by Jon Benedicto

Use is_callableto determine whether a given variable is a function. For example:

使用is_callable来确定给定的变量是否是一个函数。例如:

$func = function()
{  
    echo 'asdf';  
};

if( is_callable( $func ) )
{
    // Will be true.
}

回答by Gumbo

You can use function_existsto check there is a function with the given name. And to combine that with anonymous functions, try this:

您可以使用function_exists来检查是否存在具有给定名称的函数。并将其与匿名函数结合起来,试试这个:

function is_function($f) {
    return (is_string($f) && function_exists($f)) || (is_object($f) && ($f instanceof Closure));
}

回答by Brilliand

If you only want to check whether a variable is an anonymous function, and not a callable string or array, use instanceof.

如果只想检查变量是否是匿名函数,而不是可调用的字符串或数组,请使用instanceof.

$func = function()
{  
    echo 'asdf';  
};

if($func instanceof Closure)
{
    // Will be true.
}

Anonymous functions (of the kind that were added in PHP 5.3) are always instances of the Closureclass, and every instance of the Closureclass is an anonymous function.

匿名函数(在 PHP 5.3 中添加的那种)总是Closure类的实例,类的每个实例Closure都是匿名函数。

There's another type of thing in PHP that could arguably be considered a function, and that's objects that implement the __invokemagic method. If you want to include those (while still excluding strings and arrays), use method_exists($func, '__invoke'). This will still include closures, since closures implement __invokefor consistency.

PHP 中有另一种类型的东西可以说是一个函数,那就是实现__invoke魔法方法的对象。如果您想包含这些(同时仍不包括字符串和数组),请使用method_exists($func, '__invoke'). 这仍然包括闭包,因为闭包是__invoke为了一致性而实现的。

回答by Artemiy StagnantIce Alexeew

function is_function($f) {
    return is_callable($f) && !is_string($f);
}

回答by Andrey Izman

In php valid callables can be functions, name of functions (strings) and arrays of the forms ['className', 'staticMethod']or [$object, 'method'], so to detect only functions need to exclude strings and arrays:

在 php 中,有效的可调用对象可以是函数、函数名称(字符串)和形式的数组['className', 'staticMethod']or [$object, 'method'],因此仅检测函数需要排除字符串和数组:

function isFunction($callable) {
    return $callable && !is_string($callable) && !is_array($callable) && is_callable($callable);
}