首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

Callbacks / Callables

Callbacks can be denoted by callable type hint as of PHP 5.4. This documentation used callback type information for the same purpose.

Some functions like call_user_func() or usort() accept user-defined callback functions as a parameter. Callback functions can not only be simple functions, but also object methods, including static class methods.

Passing

A PHP function is passed by its name as a string. Any built-in or user-defined function can be used, except language constructs such as: array(), echo, empty(), eval(), exit(), isset(), list(), print or unset().

A method of an instantiated object is passed as an array containing an object at index 0 and the method name at index 1. Accessing protected and private methods from within a class is allowed.

Static class methods can also be passed without instantiating an object of that class by passing the class name instead of an object at index 0. As of PHP 5.2.3, it is also possible to pass 'ClassName::methodName'.

Apart from common user-defined function, anonymous functions can also be passed to a callback parameter.

Example #1 Callback function examples

代码语言:javascript
复制
<?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

//?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!');
?>

Example #2 Callback example using a Closure

代码语言:javascript
复制
<?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);
?>

The above example will output:

代码语言:javascript
复制
2 4 6 8 10

Note: Callbacks registered with functions such as call_user_func() and call_user_func_array() will not be called if there is an uncaught exception thrown in a previous callback.

← NULL

Pseudo-types and variables used in this documentation →

代码语言:txt
复制
 ? 1997–2017 The PHP Documentation Group

Licensed under the Creative Commons Attribution License v3.0 or later.

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com