php is_function()确定变量是否为函数 [英] php is_function() to determine if a variable is a function

查看:75
本文介绍了php is_function()确定变量是否为函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很高兴阅读php中的匿名函数,您声明的函数要比使用 create_function .现在,我想知道是否有一个传递了变量的函数,如何检查它以确定它是否是一个函数?还没有is_function()函数,当我对作为函数的变量进行var_dump时::

I was pretty excited to read about anonymous functions in 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::

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

我明白了:

object(Closure)#8 (0) { } 

关于如何检查这是否是功能的任何想法?

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

推荐答案

使用 is_callable 确定给定变量是否为函数.例如:

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

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

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

这篇关于php is_function()确定变量是否为函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆