©
Dieses Dokument verwendet PHP-Handbuch für chinesische Websites Freigeben
(PHP 4 >= 4.0.5, PHP 5)
call_user_method_array — 调用一个用户方法,同时传递参数数组(已废弃)
$method_name
, object &$obj
, array $paramarr
)call_user_method_array() 函数自 PHP 4.1.0 起已废弃,使用 call_user_func_array() 函数和 array(&$obj, "method_name") 语法代替。
从用户定义的 obj
对象中调用 method_name
所指的方法,同时使用 paramarr
中的参数。
参见 call_user_func_array() 和 call_user_func() 。
[#1] musaatalay dot mobile at gmail dot com [2014-07-15 07:57:57]
<?php
class a{
function b($a,$b,$c){
echo $a." ".$b." ".$c;
}
function c(Array $a, Array $b){
print_r($a);
echo "<br />";
print_r($b);
}
function cuf(Array $a, Array $b){
print_r($a);
echo "<br />";
print_r($b);
}
}
$a = new a;
// ### Just String Params ###
$array = array("Musa ATALAY",":","Software Developer");
$str = NULL;
foreach($array AS $v){
if(is_string($v)){
$str.="'".$v."',";
}else{
$str.=$v;
}
}
$str = rtrim($str,",");
$run = "echo \$a->b(".$str.");";
echo "<br />";
eval($run);
$str = NULL;
// ### With Array Params ###
$array = array(array("Musa ATALAY",":","Software Developer"),array("Musa ATALAY",":","Software Developer"));
foreach($array AS $i => $v){
if(is_string($v)){
$str.="'".$v."',";
}else{
$str.="\$array[".$i."],";
}
}
$str = rtrim($str,",");
$run = "echo \$a->c(".$str.");";
echo "<br />";
eval($run);
?>
[#2] wloche at hotmail dot com [2010-07-15 01:33:37]
You don't have to write a new function,
<?php call_user_func_array(array($obj, $method_name), $params); ?>
works pretty fine! (to my mind, 'eval' fucntion should be avoided almost all the time)
[#3] brudinie at googlemail dot com [2009-03-25 07:23:15]
<?php
function call_object_method_array($func, $obj, $params=false){
if (!method_exists($obj,$func)){
// object doesn't have function, return null
return (null);
}
// no params so just return function
if (!$params){
return ($obj->$func());
}
// build eval string to execute function with parameters
$pstr='';
$p=0;
foreach ($params as $param){
$pstr.=$p>0 ? ', ' : '';
$pstr.='$params['.$p.']';
$p++;
}
$evalstr='$retval=$obj->'.$func.'('.$pstr.');';
$evalok=eval($evalstr);
// if eval worked ok, return value returned by function
if ($evalok){
return ($retval);
} else {
return (null);
}
return (null);
}
?>