PHP Reference ยท Types and Type Handling
The PHP callable type
Reference for the PHP callable type: function-name strings, [object, method] arrays, closures, invokable objects, is_callable() and first-class callables.
callable is a type declaration for anything PHP can call like a function. It is not a real data type: a callable is one of several kinds of value that happen to be callable. You use it for parameters such as sort comparators, event handlers and array callbacks.
The kinds of callable are: a Closure object (from function () {} or fn() =>), the name of a function as a string, an array [$object, "method"] or ["ClassName", "staticMethod"], a string "Class::method", and any object whose class defines __invoke().
function apply(callable $fn, int $x): int { return $fn($x); } echo apply("abs", -4); // 4 echo apply(fn($n) => $n * 2, 5); // 10
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.
Syntax
function f(callable $callback): void { /* ... */ } $callback(); // direct call call_user_func($callback, $arg); // same, via function is_callable($value, false, $name); // test (and get a printable name) $fn = strlen(...); // first-class callable syntax (8.1)
| Form | Example |
|---|---|
| Function name | "strtoupper" |
| Static method | "Class::method" or ["Class", "method"] |
| Instance method | [$object, "method"] |
| Closure | function () {} or fn() => ... |
| Invokable object | $object whose class defines __invoke() |
is_callable(mixed $value, bool $syntax_only = false, ?string &$callable_name = null): bool
Usage notes
callableworks for parameters and return types, but not for properties. Declare a property asClosure(or leave it untyped) instead.- Prefer closures. Arrays and strings are easy to mistype and not checked by static analysis.
strlen(...)or$obj->method(...)creates a realClosureand fails early if the target does not exist. - The type check happens at call time and also considers visibility from the calling scope:
[$this, "privateMethod"]is callable only from inside that class. $callable()works for every form (strings, arrays, closures, invokable objects) since PHP 7, socall_user_func()is rarely needed.- Named arguments and spread work when you call a callable:
$task(...$args). - Strings from user input are dangerous. Never call
$_GET["fn"](); whitelist the allowed names. - Relative forms such as
"self::method","parent::method",["self", "method"]and["parent", "method"]are deprecated since PHP 8.2. Use a closure orself::method(...)instead. - Callbacks that need extra data should be closures with
use, or arrow functions that capture the surrounding variables automatically by value.