call_user_func和call_user_func_ar

2018-07-28  本文已影响0人  willeny
原文地址:https://blog.csdn.net/qq_27682041/article/details/78909242

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

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

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

调用没有参数的函数
<?php
    function foo(){
        return "bar";
    }
    echo foo();//调用foo()
<?php
    $function = 'no_exist_func';
    echo $function();
<?php
    echo call_user_func_array('no_exist_func',[]);
调用有参数的函数
<?php
    function foo($para1,$para2){
        return $para1.'---'.$para2;
    }
    echo foo('bar1','bar2');
<?php
    function foo($para1,$para2){
        return $para1.'---'.$para2;
    }
    echo call_user_func('foo','bar1','bar2');
<?php
    function foo($para1,$para2){
        return $para1.'---'.$para2;
    }
    echo call_user_func_array('foo',['bar1','bar2']);
面向对象时的调用
<code class="language-php"><?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. 无报错</code>

第二个: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>";
引用传递
<?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
总结:

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快。

上一篇 下一篇

猜你喜欢

热点阅读