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

查看:49
本文介绍了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天全站免登陆