猿问

关于 php 的 call_user_func()

不明白为什么要使用 call_user_func()
看了官网
http://www.php.net/manual/zh/...
http://php.net/manual/zh/func...

也还是不明白,只是说处理回调函数,网上多数是说call_user_func_array 的用法,多数用来动态传参数,但是call_user_func不处理数组参数,那么他的意义是什么呢?

看了一下样例:

<?php

class myclass {
    static function say_hello()
    {
        echo "Hello!\n";
    }
}

$classname = "myclass";

call_user_func(array($classname, 'say_hello'));
call_user_func($classname .'::say_hello'); // As of 5.2.3

输出
Hello!
Hello!

为什么命名可以用myclass::say_hello,但要偏偏使用call_user_func呢?也没看到处理回调参数的东西?

以上是我的不理解的地方,请各位帮忙解惑,谢谢。

慕村225694
浏览 373回答 1
1回答

jeck猫

自己用当然可以用具名函数和方法。如果你的函数或者方法需要接收一个回调函数作为参数呢? function test(callable $callback) { call_user_func($callback,'参数1','参数2'); }
随时随地看视频慕课网APP
我要回答