You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
63 lines
1.4 KiB
63 lines
1.4 KiB
<?php
|
|
function math($opt_name,$m,$n)
|
|
{
|
|
echo "this is a math opt '$opt_name'".'<br>';
|
|
|
|
return $opt_name($m,$n);
|
|
}
|
|
|
|
function add($m,$n)//回调函数
|
|
{
|
|
return $m+$n;
|
|
}
|
|
|
|
function subtraction($m,$n)
|
|
{
|
|
return $m-$n;
|
|
}
|
|
|
|
$ret=math('add',1,2);
|
|
echo $ret.'<br>';
|
|
|
|
$ret=math('subtraction',6,4);
|
|
echo $ret.'<br>';
|
|
|
|
/*
|
|
* add是回调函数,add作为参数传入math()中,
|
|
* 这样就可以把add(),和subtraction()公有的部分一起放入math()中,例如 echo "this is a math opt '$opt_name'".'<br>';
|
|
* 如果math作为库函数,我们是不能修改的。
|
|
* 如果不适用回调函数,那么代码将会这样
|
|
|
|
function add($m,$n)//回调函数
|
|
{
|
|
echo "this is a math opt 'add'".'<br>';
|
|
return $m+$n;
|
|
}
|
|
|
|
function subtraction($m,$n)
|
|
{
|
|
echo "this is a math opt 'subtraction'".'<br>';
|
|
return $m-$n;
|
|
}
|
|
|
|
$ret=add(1,2);
|
|
echo $ret.'<br>';
|
|
|
|
$ret=subtraction(6,4);
|
|
echo $ret.'<br>';
|
|
*/
|
|
|
|
|
|
//call_user_func()
|
|
$ret=call_user_func('add',10,5);
|
|
echo $ret.'<br>';
|
|
|
|
//call_user_func_array()
|
|
function func_b($funcName,$m,$n)
|
|
{
|
|
return call_user_func_array($funcName, array($m,$n)); //以数组的形式接收回调函数的参数
|
|
}
|
|
|
|
$ret=func_b('add',2,100);
|
|
echo $ret.'<br>';
|
|
?>
|