PHP Reference ยท Functions and Classes
Callbacks and the callable type in PHP
Reference for PHP callbacks: the five callable forms (function name, static method, object method, closure, invokable), is_callable() and first-class syntax.
A callback is a value that stands for a function you want some other code to call later. Sorting functions, array mappers, event systems and middleware all take callbacks. PHP accepts several forms for the same idea, and a parameter typed callable or checked by is_callable() accepts any of them.
Knowing the forms lets you read legacy code ('strtoupper', [$obj, 'method']) and choose the clearest style in new code, which is usually a closure or first-class callable syntax.
$upper = array_map('strtoupper', ['a', 'b']); // function name $same = array_map(fn($s) => strtoupper($s), ['a']); // closure
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
The forms of callable
| Form | Example | Notes |
|---|---|---|
| Function name | 'strtoupper' |
Any user or built-in function, as a string. |
| Static method string | 'Math::square' |
Class::method. |
| Static method array | ['Math', 'square'] |
Class name and method name. |
| Object method array | [$obj, 'cube'] |
Calls on that instance. |
| Closure | fn($x) => $x |
The preferred form. |
| Invokable object | $obj |
Its class defines __invoke(). |
| First-class callable | $obj->cube(...), strlen(...) |
PHP 8.1, creates a Closure. |
callable strings and arrays can name private or protected methods only when the callback is made from inside the class that may call them. parent::method strings were deprecated in PHP 8.2.
Usage notes
- Prefer closures and first-class syntax. They fail at the place you write them and are understood by IDEs. String callbacks only fail when called.
- Type with
callable. A bad value throwsTypeErrorat the call boundary instead of deep inside. TheClosuretype is narrower. - Check dynamic input with
is_callable(), especially names from configuration. Never pass user input as a callback name; it can call arbitrary functions. - Arguments: the caller decides what a callback receives.
array_mappasses values,array_filtercan pass keys withARRAY_FILTER_USE_BOTH,usortpasses two items. Read each function's page. - Return type expectations differ:
usortwants an int,array_filterany truthy/falsy value. - Extra arguments are fine. A callback may declare fewer parameters than supplied for user functions; built-in functions are stricter and throw
ArgumentCountError. - Calling:
$fn(...),call_user_func($fn, ...)orcall_user_func_array($fn, $args). Seecall_user_func(). - Type checks:
callablecannot be used as a property type; useClosurethere.