Skip to content

PHP Tutorial Modern PHP Features

First-Class Callables

Lesson 230 of 369 2 min read By Deepak Paswan Updated Oct 5, 2026

Before PHP 8.1, passing a function as a callback meant writing its name as a string or an array. Both are easy to mistype, and editors cannot check them.

index.php
PHP
array_map("strtoupper", $names);              // a string
array_map([$formatter, "money"], $prices);     // an array

First-class callable syntax replaces them with a real call that has (...) in place of the arguments. You get a Closure object.

index.php
PHP
<?php
$names = ["ana", "ben"];

$upper = array_map(strtoupper(...), $names);

echo implode(", ", $upper);
Output
OUTPUT
ANA, BEN

For functions

index.php
PHP
<?php
function double(int $n): int
{
    return $n * 2;
}

$fn = double(...);

echo $fn(21) . "\n";
echo get_class($fn);
Output
OUTPUT
42
Closure

For object methods

index.php
PHP
<?php
class Formatter
{
    public function __construct(private string $symbol) {}

    public function money(float $amount): string
    {
        return $this->symbol . number_format($amount, 2);
    }
}

$formatter = new Formatter("Rs. ");

$callback = $formatter->money(...);

echo implode(", ", array_map($callback, [10, 2500]));
Output
OUTPUT
Rs. 10.00, Rs. 2,500.00

The closure remembers which object it came from.

For static methods

index.php
PHP
<?php
class Text
{
    public static function shout(string $s): string
    {
        return strtoupper($s) . "!";
    }
}

$shout = Text::shout(...);

echo $shout("hello");
Output
OUTPUT
HELLO!

Why it is better

Old wayNew way
"strtoupper" (a string)strtoupper(...)
[$obj, "method"] (an array)$obj->method(...)
["Class", "method"]Class::method(...)
Typos found when the code runsTypos caught by PHP and your editor
Visibility problems appear lateRespects the visibility at the place you write it

Pass a callable to your own function

index.php
PHP
<?php
function applyAll(array $items, callable $fn): array
{
    return array_map($fn, $items);
}

print_r(applyAll(["  a ", " b"], trim(...)));
Output
OUTPUT
Array
(
    [0] => a
    [1] => b
)

It is not the same as calling the function

strlen(...) does not call strlen. It creates a callable. strlen("abc") calls it.

Combine with the pipe in PHP 8.5

In PHP 8.5, first-class callables pair with the new pipe operator |>. See PHP 8.5.

lightbulb
Use it for callbacks
Whenever you were going to write a callback as a string name or an array, use this syntax instead.