PHP中call_user_func_array()函数的用法演示
程序员文章站
2022-04-19 23:23:19
call_user_func_array (php 4 >= 4.0.4, php 5) call_user_func_array -- call a user fu...
call_user_func_array
(php 4 >= 4.0.4, php 5)
call_user_func_array -- call a user function given with an array of parameters
description
mixed call_user_func_array ( callback function, array param_arr )
call a user defined function given by function, with the parameters in param_arr. for example:
例子 1. call_user_func_array() example
<?php
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
function test($str) {
echo $str;
}
call_user_func_array("test","no.1 www.chhua.com");//输出"no.1 www.chhua.com"
//参数说明“第一个参数是函数名,第二个是参数
class testclass {
public function write($str){
echo $str;
}
}
call_user_func_array(array(testclass,write),"no.1 www.chhua.com");//用类调用的时侯,用array(),array(类名,方法名)
?>
(php 4 >= 4.0.4, php 5)
call_user_func_array -- call a user function given with an array of parameters
description
mixed call_user_func_array ( callback function, array param_arr )
call a user defined function given by function, with the parameters in param_arr. for example:
例子 1. call_user_func_array() example
复制代码 代码如下:
<?php
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
function test($str) {
echo $str;
}
call_user_func_array("test","no.1 www.chhua.com");//输出"no.1 www.chhua.com"
//参数说明“第一个参数是函数名,第二个是参数
class testclass {
public function write($str){
echo $str;
}
}
call_user_func_array(array(testclass,write),"no.1 www.chhua.com");//用类调用的时侯,用array(),array(类名,方法名)
?>
上一篇: python基础之逻辑运算符
下一篇: 按PEP8风格自动排版Python代码