©
本文檔使用 php中文網手册 發布
自 PHP 5.4 起可用 callable 类型指定回调类型 callback。本文档基于同样理由使用 callback 类型信息。
一些函数如 call_user_func() 或 usort() 可以接受用户自定义的回调函数作为参数。回调函数不止可以是简单函数,还可以是对象的方法,包括静态类方法。
一个 PHP 的函数以 string 类型传递其名称。可以使用任何内置或用户自定义函数,但除了语言结构例如: array() , echo , empty() , eval() , exit() , isset() , list() , print 或 unset() 。
一个已实例化的对象的方法被作为数组传递,下标 0 包含该对象,下标 1 包含方法名。
静态类方法也可不经实例化该类的对象而传递,只要在下标 0 中包含类名而不是对象。自 PHP 5.2.3 起,也可以传递 'ClassName::methodName'。
除了普通的用户自定义函数外, create_function() 可以用来创建一个匿名回调函数。自 PHP 5.3.0 起也可传递 closure 给回调参数。
Example #1 回调函数示例
<?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
?>
Example #2 使用 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
Note: 在 PHP 4 中,需要使用一个引用来创建一个指向具体对象的回调函数,而不是一个拷贝。参见引用的解释。
Note:
在函数中注册有多个回调内容时(如使用 call_user_func() 与 call_user_func_array() ),如在前一个回调中有未捕获的异常,其后的将不再被调用。
[#1] mariano dot REMOVE dot perez dot rodriguez at gmail dot com [2015-09-21 19:45:53]
I needed a function that would determine the type of callable being passed, and, eventually,
normalized it to some extent. Here's what I came up with:
<?php
function callableType($callable, $strict = true, callable& $norm = null) {
if (!is_callable($callable)) {
switch (true) {
case is_object($callable):
$norm = $callable;
return 'Closure' === get_class($callable) ? 'closure' : 'invocable';
case is_string($callable):
$m = null;
if (preg_match('~^(?<class>[a-z_][a-z0-9_]*)::(?<method>[a-z_][a-z0-9_]*)$~i', $callable, $m)) {
list($left, $right) = [$m['class'], $m['method']];
if (!$strict || (new \ReflectionMethod($left, $right))->isStatic()) {
$norm = [$left, $right];
return 'static';
}
} else {
$norm = $callable;
return 'function';
}
break;
case is_array($callable):
$m = null;
if (preg_match('~^(:?(?<reference>self|parent)::)?(?<method>[a-z_][a-z0-9_]*)$~i', $callable[1], $m)) {
if (is_string($callable[0])) {
if ('parent' === strtolower($m['reference'])) {
list($left, $right) = [get_parent_class($callable[0]), $m['method']];
} else {
list($left, $right) = [$callable[0], $m['method']];
}
if (!$strict || (new \ReflectionMethod($left, $right))->isStatic()) {
$norm = [$left, $right];
return 'static';
}
} else {
if ('self' === strtolower($m['reference'])) {
list($left, $right) = [$callable[0], $m['method']];
} else {
list($left, $right) = $callable;
}
if (!$strict || !(new \ReflectionMethod($left, $right))->isStatic()) {
$norm = [$left, $right];
return 'object';
}
}
}
break;
}
$norm = $callable;
return 'unknown';
}
$norm = null;
return false;
}
?>
Hope someone else finds it useful.
[#2] Riikka K [2015-05-11 13:36:15]
A note on differences when calling callbacks as "variable functions" without the use of call_user_func() (e.g. "
<?php $callback = 'printf'; $callback('Hello World!') ?>
"):
- Using the name of a function as string has worked since at least 4.3.0
- Calling anonymous functions and invokable objects has worked since 5.3.0
- Using the array structure [$object, 'method'] has worked since 5.4.0
Note, however, that the following are not supported when calling callbacks as variable functions, even though they are supported by call_user_func():
- Calling static class methods via strings such as 'foo::doStuff'
- Calling parent method using the [$object, 'parent::method'] array structure
All of these cases are correctly recognized as callbacks by the 'callable' type hint, however. Thus, the following code will produce an error "Fatal error: Call to undefined function foo::doStuff() in /tmp/code.php on line 4":
<?php
class foo {
static function callIt(callable $callback) {
$callback();
}
static function doStuff() {
echo "Hello World!";
}
}
foo::callIt('foo::doStuff');
?>
The code would work fine, if we replaced the '$callback()' with 'call_user_func($callback)' or if we used the array ['foo', 'doStuff'] as the callback instead.
[#3] edanschwartz at gmail dot com [2015-02-02 18:13:48]
You can use 'self::methodName' as a callable, but this is dangerous. Consider this example:
<?php
class Foo {
public static function doAwesomeThings() {
FunctionCaller::callIt('self::someAwesomeMethod');
}
public static function someAwesomeMethod() {
// fantastic code goes here.
}
}
class FunctionCaller {
public static function callIt(callable $func) {
call_user_func($func);
}
}
Foo::doAwesomeThings();
?>
This results in an error:
Warning: class 'FunctionCaller' does not have a method 'someAwesomeMethod'.
For this reason you should always use the full class name:
<?php
FunctionCaller::callIt('Foo::someAwesomeMethod');
?>
I believe this is because there is no way for FunctionCaller to know that the string 'self' at one point referred to to `Foo`.
[#4] Yzmir Ramirez [2014-04-15 23:40:22]
> As of PHP 5.2.3, it is also possible to pass 'ClassName::methodName'
You can also use 'self::methodName'. This works in PHP 5.2.12 for me.
[#5] computrius at gmail dot com [2013-10-11 16:38:24]
When specifying a call back in array notation (ie. array($this, "myfunc") ) the method can be private if called from inside the class, but if you call it from outside you'll get a warning:
<?php
class mc {
public function go(array $arr) {
array_walk($arr, array($this, "walkIt"));
}
private function walkIt($val) {
echo $val . "<br />";
}
public function export() {
return array($this, 'walkIt');
}
}
$data = array(1,2,3,4);
$m = new mc;
$m->go($data); // valid
array_walk($data, $m->export()); // will generate warning
?>
Output:
1<br />2<br />3<br />4<br />
Warning: array_walk() expects parameter 2 to be a valid callback, cannot access private method mc::walkIt() in /in/tfh7f on line 22
[#6] metamarkers at gmail dot com [2013-06-03 19:34:43]
you can pass an object as a callable if its class defines the __invoke() magic method..
[#7] steve at mrclay dot org [2012-09-17 21:00:06]
Performance note: The callable type hint, like is_callable(), will trigger an autoload of the class if the value looks like a static method callback.
[#8] andrewbessa at gmail dot com [2012-06-19 03:16:29]
You can also use the $this variable to specify a callback:
<?php
class MyClass {
public $property = 'Hello World!';
public function MyMethod()
{
call_user_func(array($this, 'myCallbackMethod'));
}
public function MyCallbackMethod()
{
echo $this->property;
}
}
?>