← CodeClarityLab Home
Browse by Category
+ added · updated 7d
← Back to glossary

callable vs Closure vs First-Class Callable

php PHP 7.1+ Intermediate

TL;DR

PHP has three callable forms: loose callable (string/array), typed Closure, and PHP 8.1 first-class callables (strlen(...)) — prefer Closure or first-class callables for type safety.

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

callable and Closure are the same — callable is a loose type that accepts strings and arrays too. Closure is a specific typed class.

Why It Matters

callable type hints prevent static analysis from verifying callback signatures — using Closure enables type checking of the callback's parameters and return type.

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

✗ Vulnerable
function process(callable $callback, array $items): array {
    return array_map($callback, $items); // No type safety on callback
}
process('strtolower', $items); // Works but fragile
✓ Fixed
// 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);
}

Added 22 Mar 2026
Views 22
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings F 2 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 1 ping S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 1 ping S 0 pings S 0 pings M 0 pings T 0 pings W 1 ping T 0 pings F 2 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 0 pings S
No pings yet today
No pings yesterday
Amazonbot 8 Unknown AI 3 Perplexity 3 ChatGPT 1 Google 1 Meta AI 1 Ahrefs 1
crawler 17 pre-tracking 1
DEV INTEL Tools & Severity
🔵 Info ⚙ Fix effort: Low
⚡ Quick Fix
Replace callable type hints with Closure. Use strlen(...) syntax (PHP 8.1) or Closure::fromCallable() to convert built-in functions to typed Closures.
📦 Applies To
PHP 7.1+ web cli queue-worker
🔗 Prerequisites
🔍 Detection Hints
callable \$
Auto-detectable: ✓ Yes phpstan psalm
🤖 AI Agent
Confidence: Medium False Positives: Medium ✓ Auto-fixable Fix: Low Context: Function

✓ schema.org compliant