Europe/Paris
Posts

PHP 8.6 partial function application: which closures to delete, and the ones to keep

November 30, 2026 · 4 min de lecture
PHP 8.6, stable since November 19, 2026, adds partial function application (PFA): you call a function with some arguments replaced by placeholders, and you get a Closure instead of a result. The practical question: which of the arrow functions in my code can go, and which ones must stay? The RFC by Larry Garfield and Arnaud Le Blanc defines two markers:
  • ? stands for exactly one argument, which becomes a required parameter of the generated closure.
  • ... stands for "all remaining arguments", and keeps their optionality and default values.
As soon as a call contains at least one placeholder, PHP does not call the function: it builds a Closure that stores the function and the values already supplied. That closure inherits the names, types, defaults and by-reference flags of the original parameters, so ReflectionFunction and strict typing work as usual.
Php
<?php

declare(strict_types=1);

$formatEuros = number_format(?, decimals: 2, decimal_separator: ',', thousands_separator: ' ');

echo $formatEuros(1234.5); // 1 234,50
// same as: static fn (float $num): string => number_format($num, 2, ',', ' ')
The order is fixed: positional values and ? come first, then named arguments (values or ?), then at most one .... First-class callable syntax (FCC), strtolower(...), which has existed since PHP 8.1, is in fact the degenerate case of PFA: every argument is left open. The pipe operator |>, added in PHP 8.5, passes the value on its left to the callable on its right. As Brent Roose points out on stitcher.io, it always expects a callable with a single parameter. Before 8.6, any function that needed more than one argument forced you to write an arrow function. Matthew Weier O'Phinney shows the same kind of email-list cleanup on the Zend blog; here it is inside a Symfony service:
Php
<?php

declare(strict_types=1);

namespace App\Newsletter;

final class RecipientListParser
{
    /** @return list<string> */
    public function parse(string $input): array
    {
        return explode(';', $input)
            |> array_map(trim(...), ?)
            |> array_map(strtolower(...), ?)
            |> array_filter(?, $this->isValidEmail(...))
            |> array_unique(...)
            |> array_values(...);
    }

    private function isValidEmail(string $email): bool
    {
        return false !== filter_var($email, FILTER_VALIDATE_EMAIL);
    }
}
The PHP 8.5 version needed a parenthesised arrow function at each step, |> (fn (array $emails): array => array_map(trim(...), $emails)), which drowns out the intent. The RFC also states that a PFA on the right of |> that is known to take exactly one argument, such as foo(?), foo(1, ?) or foo(1, ...), compiles to a direct call, with no intermediate closure. And the PHP 8.6 release notes announce that array_map() calls whose callback is an FCC or a PFA are compiled into loops. PFA also works on instance methods (the closure stays bound to the object) and static methods. A typical case is a translator called in a loop with the same domain:
Php
<?php

declare(strict_types=1);

namespace App\Invoice;

use Symfony\Contracts\Translation\TranslatorInterface;

final readonly class InvoiceStatusLabels
{
    public function __construct(private TranslatorInterface $translator)
    {
    }

    /** @return list<string> */
    public function labels(InvoiceStatus ...$statuses): array
    {
        $translate = $this->translator->trans(?, domain: 'invoices');

        return array_map(
            $translate,
            array_map(static fn (InvoiceStatus $status): string => 'invoice.status.' . $status->value, $statuses),
        );
    }
}
$translate has a single parameter, string $id. $parameters and $locale are not exposed and receive their default value. The inner arrow function stays: it concatenates a string, which no existing function does on its own. That is the right rule of thumb. PFA replaces closures that only forward arguments; as soon as there is logic, keep the closure. Named placeholders also let you reorder parameters, which helps with the array_* functions and their inconsistent argument order. The Zend post shows array_reduce(callback: ?, array: ?, ...) to get a function that takes the callback first.
  • Arguments are evaluated at creation, not at call time. $log = $this->logger->info(?, ['at' => $this->clock->now()->format(DATE_ATOM)]); freezes the date once and for all, and every message logged through $log will carry the same one. The RFC and Matthew Weier O'Phinney are explicit about it: if an argument must be computed on each call, a classic closure is still required.
  • ... forwards extra arguments. Without ..., extra arguments passed to the closure are ignored. With ..., they reach the function and can land in an optional parameter. The RFC's example: array_find() calls its callback with the value and the key, so with intval(...) the key ends up in $base, exactly as with a plain FCC. intval(?) ignores it.
  • No new, and a few forbidden functions. new Money(?, Currency::EUR) is not supported (the RFC points to lazy objects, added in PHP 8.4, for the rare use cases); go through a static factory, Money::of(?, Currency::EUR), which works. Functions that depend on the calling context, such as compact(), extract(), func_get_arg(), func_get_args() and get_defined_vars(), cannot be partially applied either, and neither can __get and __set.
Before adopting the syntax on a project, check that your PHP minimum version is really 8.6 in composer.json (the config.platform.php key included, if you set it), otherwise nothing stops the project from being installed on PHP 8.5, which cannot even parse these files. The same applies to your static analysis and Rector versions: a parser that does not know ? as an argument will fail on the first file. If you are upgrading anyway, the session default changes in the same release are covered in this earlier post.
On this page
Book a call