Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

A callable in PHP is a pseudo-type used to denote a variable that can be invoked as a function. It acts as a type declaration for arguments or return values, ensuring the provided value resolves to an executable routine within the PHP engine. Internally, PHP evaluates callables to either a Closure object or a specific string/array structure that the engine’s invocation handlers can resolve.

Callable Structures and Syntax

PHP recognizes several distinct data structures as valid callables: 1. Standard Functions Passed as a string containing the exact, fully qualified name of the function.
$callable = 'strlen';
$callable = '\Namespace\function_name';
2. Anonymous Functions (Closures) Functions without a specified name, which PHP internally compiles into instances of the Closure class.
$callable = function(int $x): int {
    return $x * 2;
};
3. Arrow Functions A concise syntax for closures that automatically captures variables from the parent scope by value.
$callable = fn(int $x): int => $x * 2;
4. Object Methods Passed as a sequentially indexed array containing exactly two elements: the object instance at index 0, and the method name as a string at index 1.
$obj = new MyClass();
$callable = [$obj, 'methodName'];
5. Static Class Methods Passed either as a two-element array containing the class name (string) and method name (string), or as a single concatenated string using the scope resolution operator (::).
$callable = ['MyClass', 'staticMethodName'];
$callable = 'MyClass::staticMethodName';
6. Invokable Objects An instance of a class that implements the __invoke() magic method. The object itself is passed as the callable.
class InvokableClass {
    public function __invoke(int $x) {
        return $x;
    }
}
$callable = new InvokableClass();
7. First-Class Callable Syntax (PHP 8.1+) A syntax that creates an anonymous function (Closure) from any existing callable using the (...) operator. This provides static analysis benefits and scope resolution at the time of creation rather than execution.
$callable = strlen(...);
$callable = $obj->methodName(...);
$callable = MyClass::staticMethodName(...);

Invocation and Verification

A variable typed as callable can be executed using variable function syntax or built-in execution functions.
// Variable invocation syntax
$result = $callable($arg1, $arg2);

// Built-in execution functions
$result = call_user_func($callable, $arg1, $arg2);
$result = call_user_func_array($callable, [$arg1, $arg2]);
To verify if a variable holds a valid callable structure before execution, PHP provides the is_callable() function. This function evaluates the structure and returns a boolean indicating whether the contents can be invoked within the current execution scope.
if (is_callable($variable)) {
    $variable();
}
Master PHP with Deep Grasping Methodology!Learn More