您的位置:首页 > 编程语言 > PHP开发

php call_user_func和call_user_func_array

2013-06-22 22:33 573 查看
首先要看这个页面关于callable类型:http://www.php.net/manual/zh/language.types.callable.php

自 PHP 5.4 起可用 callable 类型指定回调类型 callback。本文档基于同样理由使用 callback 类型信息。

一些函数如 call_user_func()usort() 可以接受用户自定义的回调函数作为参数。回调函数不止可以是简单函数,还可以是对象的方法,包括静态类方法。

传递

一个 PHP 的函数以 string 类型传递其名称。可以使用任何内置或用户自定义函数,但除了语言结构例如: array()echoempty()eval()exit()isset()list()printunset()

一个已实例化的对象的方法被作为数组传递,下标 0 包含该对象,下标 1 包含方法名。

静态类方法也可不经实例化该类的对象而传递,只要在下标 0 中包含类名而不是对象。自 PHP 5.2.3 起,也可以传递 'ClassName::methodName'。

除了普通的用户自定义函数外, create_function() 可以用来创建一个匿名回调函数。自 PHP 5.3.0 起也可传递 closure 给回调参数。

<?php

// An example callback function
function my_callback_function() {
echo 'hello world!';
}

// An example callback method
class MyClass {
static function myCallbackMethod() {
echo 'Hello World!';
}
}

// Type 1: Simple callback
call_user_func('my_callback_function');

// Type 2: Static class method call
call_user_func(array('MyClass', 'myCallbackMethod'));

// Type 3: Object method call
$obj = new MyClass();
call_user_func(array($obj, 'myCallbackMethod'));

// Type 4: Static class method call (As of PHP 5.2.3)
call_user_func('MyClass::myCallbackMethod');

// Type 5: Relative static class method call (As of PHP 5.3.0)
class A {
public static function who() {
echo "A\n";
}
}

class B extends A {
public static function who() {
echo "B\n";
}
}

call_user_func(array('B', 'parent::who')); // A
?>


使用 Closure 的示例

<?php
// Our closure
$double = function($a) {
return $a * 2;
};

// This is our range of numbers
$numbers = range(1, 5);

// Use the closure as a callback here to
// double the size of each element in our
// range
$new_numbers = array_map($double, $numbers);

print implode(' ', $new_numbers);
?>


输出:

2 4 6 8 10

参考:http://www.php.net/manual/zh/language.types.callable.php

一篇文章:

call_user_func函数是当需要动态调用函数时,才使用的,这个函数有两种用法:
第一种是调用孤独的函数:

<!--?
function funa($b,$c)
{
echo $b;
echo $c;
}
call_user_func('funa', "111","222");
call_user_func('funa', "333","444");
//显示 111 222 333 444
//大家有没有发现,这个用法有点像javascript中的call方法,嘿嘿
?-->


第二种是调用类内部的函数

<!--?
class a {
function b()
{
$args = func_get_args();
$num = func_num_args();
print_r($args);
echo $num;
}
}
call_user_func(array("a", "b"),"111","222");
?-->


上面这个例子,自己运行一下看看结果是什么吧~嘿嘿~提示一下func_get_args()函数是获取传入到函数中的参数,返回一个数组,func_num_args()函数获取传入函数中的参数的个数。

下面再来看看
call_user_func_array函数
这个函数也是当需要动态调用函数时用到的,它的用法和call_user_func函数比较像,只是参数传入的是数组。

<!--?
function a($b, $c)
{
echo $b;
echo $c;

}
call_user_func_array('a', array("111", "222"));
//显示 111 222
?-->


<!--?
Class ClassA
{

function bc($b, $c) {
$bc = $b + $c;
echo $bc;
}
}
call_user_func_array(array('ClassA','bc'), array("111", "222"));
//显示 333
?-->


一个调用例子:

<!--?
function otest1 ($a)
{
echo( '一个参数' );
}

function otest2 ( $a, $b)
{
echo( '二个参数' );
}

function otest3 ( $a ,$b,$c)
{
echo( '三个啦' );
}

function otest (){
$args = func_get_args();
$num = func_num_args();
call_user_func_array( 'otest'.$num, $args  );
}
otest("11");
otest("11","22");
otest("11","22","33");


一个很奇怪的问题,我下面有一个文件;

<?php
namespace Foo\Bar\subnamespace;

class foo
{

public function printArray(array $arr)
{
var_dump($arr);
}
}

$foo1=new foo();
call_user_func_array(array($foo1,'printArray'),array(1,2));


报错:

Catchable fatal error: Argument 1 passed to Foo\Bar\subnamespace\foo::printArray() must be of the type array, integer given inF:\xampp\htdocs\php\pattern\namespace\file1.php on line 9

我传递的的确是数组啊。

为什么printArray说不是数组,换成call_user_func:

call_user_func(array($foo1,'printArray'),array(1,2));

竟然正常了。这个无法解释。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: