Callback / Callable 类型
- 自 PHP 5.4 起可用 callable 类型指定回调类型 callback
- 回调函数不止可以是简单函数,还可以是对象的方法,包括静态类方法。
类型传递
- PHP是将函数以string形式传递的,可以使用任何内置或用户自定义函数。
但除了语言结构 例如:
array(),echo,empty(),eval(),exit(),isset(),list(),print 或unset()
- 一个已实例化的 object 的方法被作为 array 传递。
下标 0 包含该 object,下标 1 包含方法名
在同一个类里可以访问 protected 和 private 方法。
- 静态类方法也可不经实例化该类的对象而传递。
只要在下标 0 中包含类名而不是对象。
自 PHP 5.2.3 起,也可以传递 ‘ClassName::methodName’。
- 除了普通的用户自定义函数外,也可传递 匿名函数 给回调参数
代码
- 回调函数示例
// 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
// Type 6: Objects implementing __invoke can be used as callables (since PHP 5.3)
class C {
public function __invoke($name) {
echo 'Hello ', $name, "\n";
}
}
$c = new C();
call_user_func($c, '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
Note:
在函数中注册有多个回调内容时(如使用 call_user_func() 与 call_user_func_array()),如在前一个回调中有未捕获的异常,其后的将不再被调用。