PHP callable Type
last modified November 25, 2026
The PHP callable type describes any value that can be invoked as a function. Function names, method arrays, closures, and invokable objects all fit, which lets one function accept a piece of behaviour as an argument.
This article covers the callable forms one by one, the callable parameter type, the first-class callable syntax added in PHP 8.1, the is_callable check, and the difference between callable and Closure.
Basic definitions
A callable is not a class and not an interface. It is a type accepted by parameters and return types, and PHP verifies the value when the function is called. A string such as "strtoupper", an array such as [$object, "method"], a Closure, and an object with an __invoke() method are all callables.
The forms differ in how the target is resolved. A string naming a function is looked up when the callable is invoked, so a typo is not caught while the file is compiled. The first-class callable syntax, written as strtoupper(...), resolves the function when the expression is evaluated and produces a Closure.
Closure is a class, and every closure is callable, but the reverse is not true: "strlen" is callable without being a Closure. The is_callable() function tests a value without invoking it, which matters for callbacks that arrive from configuration or user input.
A callback is simply a callable that is passed to another function to be called later. The PHP closures and PHP __invoke method articles cover the two object-based forms in more detail.
A function name as a string
The simplest callable is the name of a function stored in a variable. PHP calls it a variable function: the value of the variable supplies the name.
<?php
declare(strict_types=1);
function shout(string $text): string
{
return strtoupper($text) . "!";
}
$job = "shout";
echo $job("hello there"), "\n";
$job holds the string "shout", and the parentheses after it call the function with that name. The function must already be defined when the call happens; unlike a direct call, the name is not checked while the file is compiled. This form is short, but an editor cannot follow the name and a typo surfaces only at run time.
$ php callable_basic.php HELLO THERE!
The callable parameter type
A parameter declared as callable accepts any of the forms and rejects everything else before the function body runs.
<?php
declare(strict_types=1);
function apply(callable $callback, string $value): string
{
return $callback($value);
}
function shout(string $text): string
{
return strtoupper($text) . "!";
}
function whisper(string $text): string
{
return strtolower($text) . "...";
}
echo apply("shout", "Hello there"), "\n";
echo apply("whisper", "Hello there"), "\n";
apply() does not know or care which function it receives. The two calls pass different function names, and the same body produces different results. The type declaration documents the contract and makes a mistake such as passing an integer a TypeError rather than a confusing "call to undefined function" later.
$ php callable_parameter.php HELLO THERE! hello there...
Object and static methods
Methods use an array of two elements: the object and the method name. A static method can also use a class name string in the first position, or the "Class::method" form.
<?php
declare(strict_types=1);
class Formatter
{
public function __construct(private string $suffix)
{
}
public function format(string $text): string
{
return $text . $this->suffix;
}
public static function upper(string $text): string
{
return strtoupper($text);
}
}
$formatter = new Formatter(" (draft)");
$callbacks = [
[$formatter, "format"],
[Formatter::class, "upper"],
"Formatter::upper",
];
foreach ($callbacks as $callback) {
echo $callback("hello there"), "\n";
}
The first callback keeps the $formatter object, so the instance method sees its $suffix property. The other two point at the static method and work without an instance. PHP 8 removed the old behaviour that silently called an instance method statically, so a non-static method must always be paired with an object.
$ php callable_methods.php hello there (draft) HELLO THERE HELLO THERE
Closures and invokable objects
A closure is an anonymous function that can be stored in a variable and passed around. An invokable object wraps behaviour in a class and exposes it through __invoke().
<?php
declare(strict_types=1);
$prefix = "Note";
$closure = function (string $text) use ($prefix): string {
return $prefix . ": " . $text;
};
class Templater
{
public function __construct(private string $template)
{
}
public function __invoke(string $text): string
{
return str_replace("{text}", $text, $this->template);
}
}
$callbacks = [
"closure" => $closure,
"invokable" => new Templater("[{text}]"),
];
foreach ($callbacks as $kind => $callback) {
echo $kind, ": ", $callback("hello there"), "\n";
}
The closure captures $prefix explicitly with use, so the variable does not have to be global. Templater stores its template in a property and uses it on every call, which is what makes an invokable object useful for stateful callbacks. Both entries in the array are called with the same syntax, even though one is a closure and the other is an object.
$ php callable_objects.php closure: Note: hello there invokable: [hello there]
First-class callable syntax
PHP 8.1 added the (...) syntax, which turns a function or method into a closure without writing a wrapper. It is the modern replacement for function-name strings in most code.
<?php
declare(strict_types=1);
$upper = strtoupper(...);
$length = strlen(...);
echo $upper("hello there"), "\n";
echo $length("hello there"), "\n";
$names = ["alice", "bob", "carol"];
echo implode(", ", array_map(ucfirst(...), $names)), "\n";
strtoupper(...) produces a Closure that wraps the built-in function, and strlen(...) does the same for the length function. The syntax works with methods too: $object->method(...) and ClassName::method(...). Because the target is resolved where the expression appears, a misspelled function is an error at that line instead of a failure inside a later call, and static analysis tools can follow it.
$ php callable_first_class.php HELLO THERE 11 Alice, Bob, Carol
Checking a callable
is_callable() answers whether a value can be invoked, without invoking it. It is the right tool when a callback comes from an untyped source such as a configuration array.
<?php
declare(strict_types=1);
$candidates = [
"strtoupper",
"str_toupper",
[new DateTimeImmutable(), "format"],
static fn(string $text): string => $text,
];
foreach ($candidates as $candidate) {
echo is_callable($candidate) ? "callable\n" : "not callable\n";
}
function run(callable $callback, string $value): string
{
return $callback($value);
}
try {
run("str_toupper", "hello there");
} catch (TypeError $e) {
echo get_class($e), ": ", $e->getMessage(), "\n";
}
"strtoupper" exists, "str_toupper" does not, and the DateTimeImmutable::format method and the arrow function are both callable. The last part passes the invalid name to a callable parameter, and PHP raises a TypeError before the body of run() starts. Inside a function that already declares callable, a second is_callable() check is redundant.
$ php callable_check.php callable not callable callable callable TypeError: run(): Argument #1 ($callback) must be of type callable, string given, called in /app/callable_check.php on line 22
callable versus Closure
The two types are not interchangeable in a declaration. callable accepts every form, while Closure accepts only closure objects. A property cannot be declared callable at all, because a callable is not a single concrete type.
<?php
declare(strict_types=1);
function schedule(Closure $callback, string $value): string
{
return $callback($value);
}
$later = static fn(string $text): string => "Later: " . $text;
echo schedule($later, "hello there"), "\n";
try {
schedule("strtoupper", "hello there");
} catch (TypeError $e) {
echo get_class($e), ": ", $e->getMessage(), "\n";
}
schedule() declares Closure, so the arrow function is accepted and the string is rejected with a TypeError. Use callable for a parameter that may receive any callable, and Closure when the code needs closure features such as bindTo() or when the value is stored in a property. The error message names the parameter and both types, which makes the mismatch easy to fix.
$ php callable_vs_closure.php Later: hello there TypeError: schedule(): Argument #1 ($callback) must be of type Closure, string given, called in /app/callable_vs_closure.php on line 15
A practical callback: usort
Sorting functions are the most common place where a callable appears. usort() takes the array by reference and a comparator that decides the order of two elements.
<?php
declare(strict_types=1);
$people = [
["name" => "Alice", "age" => 30],
["name" => "Bob", "age" => 25],
["name" => "Cara", "age" => 35],
];
usort($people, static fn(array $a, array $b): int => $a["age"] <=> $b["age"]);
foreach ($people as $person) {
echo $person["name"], " (", $person["age"], ")\n";
}
The comparator must return a negative number when the first argument belongs before the second, zero when they are equal, and a positive number otherwise. The spaceship operator <=> produces exactly those three values, so $a["age"] <=> $b["age"] sorts by age ascending. Since PHP 8.0, usort() is stable, which means people of the same age keep their original order. The PHP array sorting article covers the other sorting functions and the spaceship operator has its own page.
$ php callable_usort.php Bob (25) Alice (30) Cara (35)
Best practices
Prefer the narrowest type that does the job, and make the callback's origin obvious at the call site.
- Declare parameters as
callableunless the code needsClosuremethods, and useClosurefor properties. - Write
strlen(...)instead of"strlen"so errors appear where the callback is created and tools can follow it. - Never pass raw user input to a callable parameter; a string names any function in the program, including dangerous ones.
- Keep comparators small and return the spaceship result instead of building if/else chains.
- Use
is_callable()only at the boundary where untyped data enters, not after acallabletype declaration.
The PHP arrow functions article shows the short closure syntax used for comparators, and PHP array functions covers the callbacks that array_map and array_filter expect.
Source
PHP manual: first-class callable syntax
This article covered the PHP callable type: function-name strings, method arrays, closures, invokable objects, first-class callable syntax, validation with is_callable(), and the distinction from Closure. Continue with PHP closures for capturing variables, or read PHP array sorting for comparators in practice.
Author
List all PHP tutorials.