W3docs

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.

FormExample valueCalls
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 fnfunction () { ... }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 __INVOKE

Checking 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;           // 2020

Because 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.
  • callable cannot be a property type before PHP 8.2 with caveats. Using callable as a class property type is not allowed; store a Closure (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 TypeError instead 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

Practice

Practice
What does PHP's 'callable' denote?
What does PHP's 'callable' denote?
Was this page helpful?