PHP 函数之 call_user_func & call_user_func_array

WBOY
Release: 2016-06-23 13:53:01
Original
1450 people have browsed it

call_user_func_array (callable $callback, array $param_arr)

参数1: 调用一个回调函数, 

参数2: 数组参数是回调函数的参数.


call_user_func(callable $callback, $mixed $parameter, $mixed $...)

参数1:调用的回调函数

参数2-n:回调函数的参数.


比较这两者的不同哦. 前者的第二个参数必须是 数组.


情况一:调用普通的函数.

<?phpfunction barber($type){    echo "You wanted a $type haircut, no problem\n";}call_user_func('barber', "mushroom");call_user_func('barber', "shave");?> 
Copy after login

这里,第一个参数直接指定函数的名称.

<?phpfunction barber($type){    echo "You wanted a $type haircut, no problem\n";}call_user_func_array('barber', array("mushroom"));call_user_func_array('barber', array("shave"));?> 
Copy after login

还是比较一下这两者之间的不同.

看出来的请继续.


情况二: 调用类中的静态函数.

下面是call_user_func_arr

namespace Foo;class F {	public static function showName ($name) {		return strtoupper($name);	}}echo call_user_func_array(__NAMESPACE__.'\F::showName', array('vein'));echo call_user_func_array(array(__NAMESPACE__.'\F', 'showName'), array('vein'));$f = new F();echo call_user_func_array(array($f, 'showName'), array('vein'));
Copy after login

下面是call_user_func 

namespace Foo;class F {	public static function showName ($name) {		return strtoupper($name);	}}echo call_user_func(__NAMESPACE__ . '\F::showName','vein');echo call_user_func(array(__NAMESPACE__ . '\F','showName'),'vein');$f = new F();echo call_user_func(array($f, 'showName'),'vein');	
Copy after login

情况三:动态方法调用

call_user_func

namespace Foo;class F {	public function showAge ($age) {		return $age + 100;	}}call_user_func(__NAMESPACE__ . '\F::showAge',23);
Copy after login

这里要注意一点,如果这样调用的话,系统会报错,提示

call_user_func() expects parameter 1 to be a valid callback, non-static method Foo\F::showAge() should not be called s
tatically

解释一下: 这个函数的第一个参数必须是 有效的回调函数, 非静态的方法showAge() 是不允许调用的.

需要的解决方案是:

namespace Foo;class F {	public static function showName ($name) {		return strtoupper($name);	}	public function showAge ($age) {		return $age + 100;	}}$f = new F(); echo call_user_func(array($f, 'showAge'),23);
Copy after login


对于这样的动态函数的调用,必须提前进行对象实例化,

然后将实例化之后的对象传入函数作为第一个参数.


call_user_func_array :

namespace Foo;class F {	public static function showName ($name) {		return strtoupper($name);	}	public function showAge ($age) {		return $age + 100;	}}$f = new F(); echo call_user_func_array(array($f, 'showAge'),array(23));
Copy after login

这样也是可行的.


总结一下: call_user_func_array 和 call_user_func 两个函数基本上是类似的,只是在调用上传递参数时存在一些差异.

记住一点,call_user_func_array 传递的第二个参数必须是数组,

call_user_func 传递的第二个参数可能是数组.如果是多个参数的话,还是需要以列表的形式列出.



Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!