callable
Learn the PHP callable type hint — callback forms (function name, method, closure, __invoke), how is_callable() works, and common gotchas.
The PHP callable Type Hint
A callback is any value PHP can invoke as if it were a function — a plain function name, a method on an object, a closure, and so on. The callable type hint lets you declare that a parameter, property, or return value must be one of these invocable values. Combined with a real call later ($callback(...)), it is the foundation of higher-order functions like array_map(), usort(), and array_filter().
This page covers the syntax, every form a callable can take, how to validate one at runtime with is_callable(), and the gotchas that trip people up.
Syntax
Add callable before the parameter name (or as a property/return type) to require an invocable value:
<?php
function run(callable $callback) {
// $callback is guaranteed to be invocable here
return $callback();
}If the caller passes something that is not callable, PHP throws a TypeError before your function body runs — so inside the function you can rely on $callback() working.
The five forms of a callable
callable accepts more than just a function name. Each row below is a different value PHP recognises as a callback.
| Form | Example value | Calls |
|---|---|---|
| Function name (string) | 'strtoupper' | a named function |
| Static method (string) | 'MyClass::myMethod' | a static method |
| Static method (array) | ['MyClass', 'myMethod'] | a static method |
| Instance method (array) | [$object, 'myMethod'] | a method on $object |
| Closure / arrow fn | function () { ... } | an anonymous function |
| Invokable object | $obj with __invoke() | the object's __invoke() |
The example below exercises all of them with a single run() helper:
<?php
function run(callable $callback): string
{
return $callback();
}
// 1. A named function passed by string
function greet(): string
{
return "Hello world!";
}
echo run('greet') . PHP_EOL;
// 2 & 3. Instance method and static method
class Greeter
{
public function instanceHello(): string
{
return "Hello from an instance";
}
public static function staticHello(): string
{
return "Hello from a static method";
}
}
$greeter = new Greeter();
echo run([$greeter, 'instanceHello']) . PHP_EOL; // instance method
echo run(['Greeter', 'staticHello']) . PHP_EOL; // static method
echo run('Greeter::staticHello') . PHP_EOL; // static method as a string
// 4. A closure (anonymous function)
echo run(function (): string {
return "Hello from a closure";
}) . PHP_EOL;
// 5. An invokable object (has an __invoke method)
class Loud
{
public function __invoke(): string
{
return "HELLO FROM __INVOKE";
}
}
echo run(new Loud()) . PHP_EOL;Running this prints:
Hello world!
Hello from an instance
Hello from a static method
Hello from a static method
Hello from a closure
HELLO FROM __INVOKEChecking a value with is_callable()
A type hint validates input at the function boundary, but sometimes you receive a value dynamically (from config, user input, or a registry) and want to check it before calling. Use is_callable():
<?php
$maybe = 'strtoupper';
if (is_callable($maybe)) {
echo $maybe('hi'), PHP_EOL; // HI
} else {
echo "Not callable", PHP_EOL;
}
var_dump(is_callable('strtoupper')); // bool(true)
var_dump(is_callable('no_such_function')); // bool(false)
var_dump(is_callable([new DateTime(), 'format'])); // bool(true)is_callable() returns true only if the target actually exists and is reachable — a misspelled function name returns false instead of crashing, which makes it ideal for plugin-style code.
First-class callable syntax (PHP 8.1+)
Since PHP 8.1 you can turn any function or method into a Closure with the (...) syntax. This is type-safe, IDE-friendly, and avoids fragile strings:
<?php
$upper = strtoupper(...); // a Closure wrapping strtoupper
echo $upper('hello'), PHP_EOL; // HELLO
$greeter = new DateTime('2020-01-01');
$fmt = $greeter->format(...); // bound to $greeter
echo $fmt('Y'), PHP_EOL; // 2020Because the result is a Closure, it satisfies the callable type hint everywhere a callback is expected.
Common gotchas
- Private/protected methods. A
[$object, 'method']callable only works if the method is visible from where it is invoked. Calling a private method via a string/array from outside its class fails. callablecannot be a property type before PHP 8.2 with caveats. Usingcallableas a class property type is not allowed; store aClosure(or\Closure) instead, or accept it as a parameter.- Static call without an object.
[$object, 'staticMethod']works, but for clarity prefer['ClassName', 'staticMethod']or'ClassName::staticMethod'for static targets. See static methods. - Strings vs. objects. A string callable like
'Greeter::staticHello'only works for static methods, not instance methods — instance calls need the[$object, 'method']array form.
Why use callable?
- Type safety: PHP rejects invalid callbacks at the call site with a clear
TypeErrorinstead of failing deep inside your function. - Self-documenting signatures: readers (and tools like PHPStan or Psalm) immediately see that a parameter is "something I will call."
- Flexibility: the same function accepts a function name, a method, or a closure — so callers choose the most convenient form.
See also
- PHP callback functions — using callables with
array_map,usort, and friends. - PHP functions — defining the functions you pass as callbacks.
- PHP static methods — the static-method callable forms.
- PHP data types — where
callablefits among PHP's types.