call_user_func和call_user_func_array

常用的两个回调函数的学习:call_user_funccall_user_func_array
mixed call_user_func ( callback function [, mixed parameter [, mixed ...]] ):
利用第一个参数回调用户的函数

利用给定函数的参数来调用一个用户定义的函数:
Call a user defined function given by the function parameter. Take the following:
例子:
function barber($type)
{
   echo "You wanted a $type haircut, no problem";
}
call_user_func('barber', "mushroom");
call_user_func('barber', "shave");

类的方法也可以通过传递参数"array(类名,方法名)"的方式静态调用.
Class methods may also be invoked statically using this function by passing array($classname, $methodname) to the function parameter.
例如:
class myclass {
   function say_hello()
   {
       echo "Hello!/n";
   }
}

$classname = "myclass";

call_user_func(array($classname, 'say_hello'));

注意:
call_user_func()的参数不是以引用的方式传递的。(???????)
Note that the parameters for call_user_func() are not passed by reference.

function increment(&$var)
{
   $var++;
}
$a = 0;
call_user_func('increment', $a);
echo $a; // 0
call_user_func_array('increment', array(&$a)); // You can use this instead
echo $a; // 1
//这个例子能说明什么问题??我只看到call_user_func_array也不是以引用的方式传递的。
//并且,如果类似的把call_user_func_array('increment', array(&$a));的第二个参数
//换成:array($a)的话,$a也是不改变的。同理,如果把 call_user_func('increment', $a);
//的第二个参数换成&$a的话,$a的值也是会+1的。



mixed call_user_func_array ( callback function, array param_arr )
:Call a user function given with an array of parameters

调用一个用户定义的函数,参数以('函数名','函数参数数组')的方式定义。

Call a user defined function given by function, with the parameters in param_arr. For example:

function debug($var, $val)
{
   echo "***DEBUGGING/nVARIABLE: $var/nVALUE:";
   if (is_array($val) || is_object($val) || is_resource($val)) {
       print_r($val);
   } else {
       echo "/n$val/n";
   }
   echo "***/n";
}

$c = mysql_connect();
$host = $_SERVER["SERVER_NAME"];

call_user_func_array('debug', array("host", $host));
call_user_func_array('debug', array("c", $c));
call_user_func_array('debug', array("_POST", $_POST));

### PHP `call_user_func_array` 使用方法及常见问题 #### 1. 函数定义与语法 `call_user_func_array` 是 PHP 中用于调用回调函数的内置函数,它允许将参数作为数组传递给目标函数。其语法如下: ```php call_user_func_array(callable $callback, array $param_arr): mixed ``` 此函数的第一个参数是一个可调用的回调函数,可以是字符串形式的函数名、数组形式的类方法或匿名函数等。第二个参数是一个索引数组,包含需要传递给回调函数的所有参数[^1]。 #### 2. 示例代码 以下是一个使用 `call_user_func_array` 的示例: ```php function sum($a, $b) { return $a + $b; } $params = [3, 5]; $result = call_user_func_array('sum', $params); echo $result; // 输出 8 ``` 在上述代码中,`call_user_func_array` 将 `$params` 数组中的值分别作为参数传递给 `sum` 函数,并返回结果。 #### 3. 常见问题及其解决方法 ##### 3.1 参数引用问题 当目标函数需要引用参数时,直接使用 `call_user_func_array` 可能会导致参数无法正确传递。为了解决这一问题,可以通过引用传递参数的方式实现[^3]。例如: ```php function modify(&$value) { $value += 10; } $param = [5]; call_user_func_array('modify', $param); echo $param[0]; // 输出 15 ``` ##### 3.2 类方法调用问题 如果需要调用类中的方法,可以将第一个参数设置为包含类名方法名的数组。例如: ```php class Calculator { public function multiply($a, $b) { return $a * $b; } } $calculator = new Calculator(); $params = [4, 6]; $result = call_user_func_array([$calculator, 'multiply'], $params); echo $result; // 输出 24 ``` 在此示例中,`call_user_func_array` 成功调用了 `Calculator` 类的 `multiply` 方法[^2]。 ##### 3.3 返回值问题 `call_user_func_array` 的返回值取决于目标函数的返回值。如果目标函数未明确返回值,则默认返回 `null`。例如: ```php function greet($name) { echo "Hello, $name!"; } $params = ['World']; $result = call_user_func_array('greet', $params); var_dump($result); // 输出 NULL ``` 因此,在使用 `call_user_func_array` 时,应确保对目标函数的返回值有清晰的理解[^4]。 #### 4. 替代方案 在 PHP 5.6 及更高版本中,可以使用扩展运算符(...)替代 `call_user_func_array`,从而简化代码并提高性能。例如: ```php function sum($a, $b) { return $a + $b; } $params = [3, 5]; $result = sum(...$params); echo $result; // 输出 8 ``` 扩展运算符能够更直观地将数组元素作为独立参数传递给函数[^4]。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值