What PHP’s Partial Function Application RFC Brings to the Language
The RFC introduces Partial Function Application (PFA) for PHP, allowing developers to pre‑bind some arguments of a callable using placeholders like ? and ... which generate a closure that preserves type information, optionality, defaults, and attributes, simplifying pipeline and functional code.
Introduction
Partial function application (PFA) allows calling a function with only a subset of its required arguments, returning a closure that captures the supplied arguments and expects the remaining ones. In PHP this is equivalent to manually writing a closure, but the proposal introduces a dedicated syntax using ? for a single‑parameter placeholder and ... for a variadic placeholder. function f(int $a, int $b, int $c) { } Current verbose arrow‑function approach: $partialF = fn (int $b) => f(1, $b, 3); With PFA the same call becomes:
$partialF = f(1, ?, 3);Proposal Overview
Partial‑application semantics
A call that contains one or more ? placeholders or a single ... placeholder does not invoke the function immediately. Instead the engine creates a closure whose signature matches the missing parameters, preserving name, type, optionality, default value, reference passing and the attributes #[NoDiscard] and #[SensitiveParameter].
Placeholder rules:
Parameter placeholder ? – expects exactly one argument at that position.
Variadic placeholder ... – may provide zero or more arguments.
The call may combine positional values, named arguments, and at most one .... The resulting closure inherits the following from the original callable:
Name
Type declarations
Optionality and default values
Reference passing
Presence of #[NoDiscard] and #[SensitiveParameter] Additional rules for variadic functions:
Any placeholder that falls into the variadic part becomes required.
If a positional placeholder enters the variadic part, all preceding placeholders become required.
Placeholders mapped to a variadic parameter are renamed with a numeric suffix ( $nums0, $nums1, …).
Examples
Simple function:
function stuff(int $i1, string $s2, float $f3, Point $p4, int $m5 = 0): string {}Fully abstracted call (all arguments deferred): $c = stuff(?, ?, ?, ?, ?); Equivalent generated closure:
$c = static fn (int $i1, string $s2, float $f3, Point $p4, int $m5 = 0) =>
stuff($i1, $s2, $f3, $p4, $m5);Providing some values and deferring the rest:
$c = stuff(1, 'hi', ?, ?, ?); // $i1 = 1, $s2 = 'hi'Using named arguments out of order: $c = stuff(?, ?, f3: 3.5, p4: $point); Creating a thunk (all arguments pre‑filled):
$c = stuff(1, 'hi', 3.4, $point, 5, ...);
$c = static fn (): string => stuff(1, 'hi', 3.4, $point, 5);Variadic function example:
function things(int $i1, ?float $f3 = null, Point ...$points) {}Partial application with variadic placeholders:
$c = things(?, ?, ?, ?);
$c = static fn (int $i1, ?float $f3, Point $points0, Point $points1) =>
things($i1, $f3, $points0, $points1);Using ... to create a thunk:
$c = things(...);
$c = static fn (int $i1, ?float $f3 = null, Point ...$points): string =>
things(...[$i1, $f3, ...$points]);Note: The RFC also discusses backward‑compatibility, future extensions, and voting details. The above excerpts capture the core proposal and representative examples.
RFC reference: https://wiki.php.net/rfc/partial_function_application_v2
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Open Source Tech Hub
Sharing cutting-edge internet technologies and practical AI resources.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
