callable vs Closure vs First-Class Callable
debt(d5/e3/b5/t7)
Closest to 'specialist tool catches it' (d5). The detection_hints list phpstan and psalm — both specialist static analysis tools — as the means to catch callable vs Closure misuse. The code_pattern `callable $` confirms these tools flag the type hint, but it won't be caught by a default linter or compiler error.
Closest to 'simple parameterised fix' (e3). The quick_fix says to replace callable type hints with Closure and use strlen(...) or Closure::fromCallable() — these are mostly find-and-replace substitutions within call sites, but touching multiple type hints across a component is a small refactor rather than a one-line patch.
Closest to 'persistent productivity tax' (e5 → b5). The term applies_to web, cli, and queue-worker contexts, meaning the callable type hint pattern can appear anywhere callbacks are used across the codebase. Using callable instead of Closure silently undermines static analysis across all these contexts, taxing many work streams without dominating the architecture.
Closest to 'serious trap' (t7). The misconception field directly states: 'callable and Closure are the same — callable is a loose type that accepts strings and arrays too. Closure is a specific typed class.' This contradicts how developers who know typed languages reason about function types. The common mistake of using string-based callables that break on refactoring compounds the trap, making it a serious contradiction of developer expectations.
TL;DR
Explanation
callable is a loose type hint that accepts strings ('strlen'), arrays ([$obj, 'method']), and Closures. It provides no IDE autocompletion or static analysis. Closure is a specific class for anonymous functions — preferred for type-safe callbacks. PHP 8.1 introduced first-class callables: strlen(...) creates a Closure from any callable. Closure::fromCallable() does the same in PHP 7.1+. For strict code: never type-hint callable, always use Closure or a specific interface. PHPStan/Psalm can check Closure parameter types.
Common Misconception
Why It Matters
Common Mistakes
- Type-hinting callable instead of Closure — loses static analysis.
- Using string-based callables ('strlen') that break with refactoring.
- Not knowing strlen(...) syntax (PHP 8.1) for converting built-ins to Closures.
Code Examples
function process(callable $callback, array $items): array {
return array_map($callback, $items); // No type safety on callback
}
process('strtolower', $items); // Works but fragile
// PHP 7.1+:
$fn = Closure::fromCallable('strtolower');
// PHP 8.1+ first-class callables:
$fn = strtolower(...);
$fn = $obj->method(...);
$fn = ClassName::staticMethod(...);
// Type-safe callback parameter:
function process(Closure $callback, array $items): array {
return array_map($callback, $items);
}