注册 登陆

    2019-09-24 22:45:18call_user_func和call_user_func_array的差别

    您现在的位置是: 首页 >  php >  call_user_func和call_user_func_array的差别

        很多开源的PHP框架和系统,均有使用到call_user_func和call_user_func_array这两个函数,如CodeIgniter、ThinkPHP、Discuz等等。那么,这两个函数有什么作用呢?又有什么区别呢?


    两个函数都是当需要动态调用函数时用到的,函数名不确定,参数不确定的情况下用到,至于区别下面会提到。

    大部分PHP都知道,调用一个函数,直接写函数名并和括号就可以了,如

    1、调用没有参数的函数

    普通函数调用

    <?php
    function foo(){
    return "bar";
    }
    echo foo();//调用foo()

    输出:
    bar

    但是当调用一个不存在的函数时,就会报致命的错误,如

    <?php
    $function = 'no_exist_func';
    echo $function();
    输出:
    Fatal error: Call to undefined function no_exist_func()

    而使用call_user_func调用时,如:

    <?php
    echo call_user_func('no_exist_func');
    输出:
    Warning: call_user_func() expects parameter 1 to be a valid callback, function 'no_exist_func' not found or invalid function name 

    而使用call_user_func_array调用时,注意的是这个函数要求两个参数,需要传2个参数,如果没函数,传个空数组,如:

    <?php
    echo call_user_func_array('no_exist_func',[]);
    输出:
    Warning: call_user_func_array() expects parameter 1 to be a valid callback, function 'no_exist_func' not found or invalid function name


    2、调用有参数的函数

    普通函数调用


    <?php
    function foo($para1,$para2){
    return $para1.'---'.$para2;
    }
    echo foo('bar1','bar2');

    输出:
    bar1---bar2

    call_user_func函数调用,参数传入的是字符串形式,需要几个参数,就传入几个参数,该函数可以传n个参数


    <?php
    function foo($para1,$para2){
    return $para1.'---'.$para2;
    }
    echo call_user_func('foo','bar1','bar2');
    输出:
    bar1---bar2

    call_user_func_array函数调用,参数传入的是数组形式,该函数只有2个参数

    <?php
    function foo($para1,$para2){
    return $para1.'---'.$para2;
    }
    echo call_user_func_array('foo',['bar1','bar2']);

    输出:
    bar1---bar2

    3、面向对象时的调用

    非静态方法的调用

    <?php
    class Person {
    public function getName($name,$age)
    {
    return "My name is ".$name.",I am ".$age." years old this year.";
    }
    }

    echo call_user_func(array("Person", "getName"),"PHP","20");//严格模式下,虽然能输出,但是会报错,静态方法才能这样调用;Strict standards: call_user_func() expects parameter 1 to be a valid callback, non-static method Person::getName() should not be called statically
    echo call_user_func(array(new Person(),"getName"),"GO","8");//输出My name is GO,I am 8 years old this year. 无报错

    echo call_user_func_array(array(new Person(),"getName"),array("GO","8"));//输出My name is GO,I am 8 years old this year. 无报错


    输出:
    第一个: Strict standards: call_user_func() expects parameter 1 to be a valid callback, non-static method Person::getName() should not be called statically

    第二个:My name is PHP,I am 20 years old this year.My name is GO,I am 8 years old this year.

    第三个:My name is GO,I am 8 years old this year.


    静态方法的调用

    <?php
    class Person {
    public static function getName($name,$age)
    {
    return "My name is ".$name.",I am ".$age." years old this year.";
    }
    }
    echo "use call_user_func:";
    echo "<br>";
    echo call_user_func(array("Person", "getName"),"PHP","20");
    echo "<br>";
    echo call_user_func("Person::getName","PHP","20");
    echo "<br>";
    echo call_user_func(['Person', 'getName'], "PHP","20");
    echo "<br>";
    echo call_user_func(array(new Person(),"getName"),"PHP","20");
    echo "<br>";
    echo "use call_user_func_array:";
    echo "<br>";
    echo call_user_func_array(array(new Person(),"getName"),array("GO","8"));
    echo "<br>";
    echo call_user_func_array(array("Person","getName"),array("GO","8"));
    echo "<br>";
    echo call_user_func_array(["Person","getName"],array("GO","8"));
    echo "<br>";
    输出:
    use call_user_func:
    My name is PHP,I am 20 years old this year.
    My name is PHP,I am 20 years old this year.
    My name is PHP,I am 20 years old this year.
    My name is PHP,I am 20 years old this year.
    use call_user_func_array:
    My name is GO,I am 8 years old this year.
    My name is GO,I am 8 years old this year.
    My name is GO,I am 8 years old this year.

    4、引用传递

    <?php
    function reference(&$var)
    {
    $var++;
    }
    $a = 0;
    call_user_func('reference', $a);//虽然输出值0,但是报了警告 Warning: Parameter 1 to reference() expected to be a reference, value given
    // call_user_func('reference', &$a);报致命错误 //Fatal error: Call-time pass-by-reference has been removed
    echo $a; // 0
    call_user_func_array('reference', array(&$a));
    echo $a; // 1

    输出:
    0 1

    总结:

    1、call_user_func可以传一个参数,共有n个参数,call_user_func_array只有2个参数,必须传2个参数;

    2、call_user_func传的参数是字符串形式,call_user_func_array传的参数是数组形式;

    3、call_user_func不能引用传递传参数,call_user_func_array允许引用传递;

    4、性能上call_user_func比call_user_func_array快。

关键字词: call_user_func和call_user_func_array的差别

0