call-user-func call-user-func-array哪个更快,更好些
手册给出的note 是 that the parameters for call_user_func() are not passed by reference
意思就是 参数(非函数名称) 不能使用引用的方式传递
例如: call_user_func (array('foo' , 'foo') , &$data); 这样使用会产生个warning . 并且达不到使用引用的效果.
call_user_func_array 可以支持参数引用的传递
性能是第一个快.
php问题 call-user-func-array 这个函数什么时候用?最近再看tp框架 很
call_user_func_array是个很神奇的函数,在你PHP水平成长到一定高度的时候,你就能深刻理解它的意义了。
这里我给你举两个适合用call_user_func_array的例子,帮助你理解场景一:参数数目不确定函数的参数是用户传入的,而且参数的数目不确定。如果不用call_user_func_array你会怎么做?一些PHP框架可以将$_GET参数自动映射到方法,就是这样实现的。
场景二:用于函数回调回调函数可能是一个普通函数,可能是类方法,也可能是静态方法。如果使用call_user_func_array,你就不用去判断方法的类型。
call-user-func-array的函数定义
hiphotos.baidu.com/zhidao/pic/item/7c1ed21b0ef41bd5fb5ca52c56da81cb38db3dde。 要传递参数给函数.com/zhidao/wh%3D600%2C800/sign=d133d96380d6277fe9473a3e18083308/7c1ed21b0ef41bd5fb5ca52c56da81cb38db3dde.jpg" esrc="http://g.hiphotos,作为一个索引数组。
|发布于2016-05-30 04:28
php7 linux上使用 call-user-func-array 报错
php __call()与call_user_func_array()理解 1. mixed __call ( string name, array arguments )The magic method __call() allows to capture invocation of non existing methods. That way __call() can be used to implement user defined method handling that depends on the name of the actual method being called. This is for instance useful for proxy implementations. The arguments that were passed in the function will be defined as an array in the $arguments parameter. The value returned from the __call() method will be returned to the caller of the method. 译文: 这个魔术方法允许用户调用类中不存在的方法,它用于实现那些 依赖于在被调用时的真正方法名的方法. 典型的例子是用来实现代理. 方法的参数$arguments是一个数组 ,__call()的返回值返回给方法调用者白话文: 这个方法主要是用来实现动态方法调用, 如果再一个类定义了__call()这个方法, 当用户调用这个类的一个不存在的方法时,他可以使用调用的那个不存在的方法的方法名和参数做出用户定义在__call()方法体内的相应操作,此时__call()方法的参数就是被调用的那个不存在的方法的方法名和参数例子';echo 'and the arguments are ';var_dump( $args );}}$person = new Person();$person->test( 1 , TRUE );?>程序输出引用you call method testand the arguments are array 0 => int 1 1 => boolean true2. mixed call_user_func_array ( callback function, array param_arr )Call a user defined function with the parameters in param_arr. 参数functionThe function to be called. param_arrThe parameters to be passed to the function, as an indexed array. 返回值Returns the function result, or FALSE on error. 此方法可以通过传入类名,类中得方法名和方法参数达到动态调用方法的效果例子';echo 'and the arguments are ';var_dump( $args );}} $person = new Person();call_user_func_array( array( $person , 'talk' ) , array( 'hello' ) );?>程序输出引用hello两个方法共用,实现代理模型 class Person{function talk( $sound ){echo $sound;}function __call( $method , $args ){echo 'you call method ' . $method . '';echo 'and the arguments are ';var_dump( $args );}}class PersonProxy{private $person;function __construct(){$this->person = new Person();}function __call( $method , $args ){call_user_func_array( array( $this->person , $method ) , $args );}}$person_proxy = new PersonProxy(); $person_proxy->talk( 'thank you' );程序输出引用thank yo。
call-user-func-array的函数说明
(PHP 4 >= 4.0.4, PHP 5)
call_user_func_array - 用一个数组作为参数调用一个回调函数.返回值为回调函数执行的结果或者为false(回调函数执行有error时).
PHP: 使用call-user-func的意义是什么
mixed call_user_func_array ( callback function, array param_arr )
:Call a user function given with an array of parameters
调用一个用户定义的函数,参数以('函数名','函数参数数组')的方式定义。
这里$args通过func_get_args()获得了用户调用b函数的参数(数组)。
然后让每个参数做为a函数的参数去运行。
至于这里为什么要用到这个,估计是用户调用b函数时参数不确定,可能是多个参数,这样就省的每一个参数都去写一遍调用a函数了。
一般单入口模式用到此函数的比较多一些,根据参数来包含不同文件。
call-user-func的实例说明
例-1
function increment(&$var)
{
$var++;
}
$a = 0;
call_user_func('increment', $a);
echo $a.\n;
call_user_func_array('increment', array(&$a)); // You can use this instead before PHP 5.3
echo $a.\n;
?>
输出:
0
1
例-2
{
echo You wanted a $type haircut, no problem\n;
}
call_user_func('barber', mushroom);
call_user_func('barber', shave);
?>
输出:
You wanted a mushroom haircut, no problem
You wanted a shave haircut, no problem
转载请注明出处51数据库 » call_user_func_array