【PHP】function_exists , method_exists 与 is_callable的区别

回调这两个字,想必接触过javascript的人都不会陌生.我们的php也拥有回调函数和闭包的概念. 那么在PHP中如何检查它是一个可调用的函数呢? 下面开始正式介绍一些方法,来检测这个问题。

首先介绍一个简单的一点的:

1. 检查函数是否存在,如果存在,那么就调用该函数.同时将参数附加进去.

<?php
function invoke($name){
  if(function_exists($name)){
     $args = array_slice(func_get_args(),0,1);
     call_user_func_array($name,$args);
  }
  die("no function");
}

function test(){
    echo 1;
}
invoke("test");   // 1
invoke("test2"); // no function

这里我们通过function_exists 来检测是否为一个函数.如果 为函数的话就立即调用函数。如果不为函数则die。

接下来我们如果把函数切换成一个类方法,那么我们该如何检验呢?
2.1 检查类的方法是否存在

<?php
class A{

    public    function foo(){
        echo 'foo';
    }
    protected static function bar(){
        echo 'bar';
    }
}

function isValidMethod($class,$method){
     return method_exists($class,$method) === true ?  'true' : 'false';
}

function invoke($class,$method){
    return call_user_func(array($class,$method));
}
print isValidMethod('A','foo');
print isValidMethod('A','bar');
$a = new A();
print isValidMethod($a,'foo');
print isValidMethod($a,'bar'); 

method_exists 能检查出对象是否存在指定的方法,该方法不管是 static 还是 final 还是 abstract 还是 默认状态。也不管是否为 public , private 还是 protected. 只要是符合语法的类方法都能够被检测出,但是无法检测出使用魔法方法__call的函数

2.2 无法检测出__call 调用的函数

<?php
class A{

    public function exists(){
        return true;
    }

    public function __call($name,$param =array()){

        if(strpos($name,"no") !== -1){
                $action = lcfirst(substr($name, 2));
                if(method_exists($this, $action)){
                    return !call_user_func_array(array($this,$action),$param);
                 }
        }
        return null;
    }
}

function isValidMethod($class,$method){
     return method_exists($class,$method) === true ?  'true' : 'false';
}

function invoke($class,$method){
    return call_user_func(array($class,$method));
}
$a = new A();
print isValidMethod($a,'exists'); // true
print isValidMethod($a,'noExists'); // false

var_dump(invoke($a,'exists')); //  true
var_dump(invoke($a,'noExists')); // true

这里我们可以看出来,method_exists能检测出用户定义的方法,但是不能使用call_user_func之类的方法调用那些被protected和private的方法.

接下来我们来看看一个具有丰富功能的检测函数is_callable. 实际上php有一种对象叫做Callable。凡属于这种对象的字符串,数组都具备回调的能力。

3. 1 使用 is_callable

<?php
class A{

    public function exists(){
        return true;
    }

    private function foo(){
        echo 'foo';
    }
    private static function bar(){
        echo 'bar';
    }
}

function invoke($class,$method){
    return call_user_func(array($class,$method));
}
print is_callable('A::exists'); // true 
print is_callable('invoke'); // true

$a = new A();
print is_callable(array($a,'exists')); // true

var_dump(is_callable(array($a,'foo'))); // false
var_dump(is_callable(array($a,'foo'),true)); // true
var_dump(is_callable(array($a,'foo'),false)); //false

从上面可以看出来 is_callable 可以判断出是否能够进行调用。默认第二个参数为false,表示该回调操作有权限能够被调用,而如果为true的话,表示只要能检测出存在具有回调的格式就好。
如果设置了__call 函数的话,那么检测出来的任何方法都是返回true的。所以这一点需要大家注意,谨慎使用__get ,或者你可以配合method_exists来缩小 is_callable的范围。

3.2 is_callable的第三个参数
它允许传递一个变量,给该变量赋于一个字符串格式的值。

var_dump(is_callable(array($a,'foo'),false),$name); //false

// 值大概如下
// $name = A::foo()

经过一番的学习之后,我对 is_callable的理解又有了更多的认识。在现在的php环境下,使用call_user_func 之类的函数越来越多。我们要判多出是否可以用来回调。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值