PHP continues to improve its support for modern programming techniques, and PHP 8.6 introduces several features that make everyday development more expressive. One of these additions is Partial Function Application, which provides a simpler way to reuse functions with predefined arguments.
Developers frequently pass functions as callbacks when working with arrays, collections, data transformations, validation, and other application operations. Before PHP 8.6, using an existing function with predefined arguments often required writing an anonymous function or arrow function. Although this approach worked, it sometimes introduced unnecessary code and made callback expressions harder to read.
Partial Function Application addresses this problem by allowing developers to provide some arguments in advance while leaving other arguments open for later. PHP creates a Closure that accepts the remaining arguments and executes the original function when that Closure is called.
In this article, we will explore how Partial Function Application works in PHP 8.6, understand its syntax, and see how to use it in practical application scenarios.
What Is Partial Function Application?
Partial Function Application is a programming technique in which some arguments of a function are provided in advance, while the remaining arguments are supplied later.
Consider a function that accepts three parameters:
<?php
function calculatePrice(
float $price,
float $tax,
float $discount
): float {
return ($price + ($price * $tax)) - $discount;
}
echo calculatePrice(100, 0.18, 10);Here, the function requires three arguments whenever it is called.
Suppose an application uses the same tax rate for multiple products. Instead of repeatedly passing the tax rate, we can create a reusable function with that value already configured.
Before PHP 8.6, developers could achieve this using an arrow function:
<?php
$calculateWithStandardTax = fn(float $price, float $discount) =>
calculatePrice($price, 0.18, $discount);
echo $calculateWithStandardTax(100, 10);This works correctly, but developers must manually create another function and repeat the relevant parameter information.
PHP 8.6 introduces a shorter approach using the question mark placeholder:
<?php
$calculateWithStandardTax = calculatePrice(
?,
0.18,
?
);
echo $calculateWithStandardTax(100, 10);The question marks represent the arguments that will be supplied later. PHP creates a Closure instead of immediately executing calculatePrice().
When the Closure is called with the remaining arguments, the original function executes.
This makes it easier to create reusable functions without manually writing additional callback logic.
Understanding Partial Function Application Syntax
PHP 8.6 introduces two important placeholders for Partial Function Application:
? represents one argument that will be supplied later.
... represents the remaining arguments that have not already been provided.
These placeholders allow developers to control which arguments are configured immediately and which arguments remain available for future calls.
Using the Question Mark (?) Placeholder
The question mark placeholder represents exactly one argument.
Consider the following example:
<?php
function sendNotification(
string $message,
string $channel,
string $recipient
): string {
return "Sending '$message' to $recipient through $channel";
}
$emailNotification = sendNotification(
?,
"email",
?
);
echo $emailNotification(
"Your order has been shipped",
"[email protected]"
);In this example, the channel is already configured as email.
The first and third arguments remain open. PHP creates a Closure that accepts those two arguments.
This is useful when an application repeatedly performs similar operations with only a few changing values.
One important detail is that each question mark represents one required argument in the generated Closure. This also applies when the original function parameter has a default value.
Using the Spread Placeholder (...)
The spread placeholder represents the remaining arguments that have not already been specified.
Consider a function that accepts an optional formatting configuration:
<?php
function formatMessage(
string $message,
string $prefix = "",
string $suffix = ""
): string {
return $prefix . $message . $suffix;
}
$formatWithPrefix = formatMessage(
?,
prefix: "[INFO] ",
...
);
echo $formatWithPrefix("Application started");Here, the message remains available for the future call, while the prefix is configured in advance.
The spread placeholder keeps the remaining parameters available and preserves their default values.
It is particularly useful when a function has several optional arguments and developers do not want to specify each one individually.
How Partial Function Application Works Internally
Understanding what PHP returns is important before using this feature in application development.
Partial Function Application does not execute the original function immediately. Instead, PHP creates a Closure representing the partially configured function.
Consider this example:
<?php
function multiply(int $number, int $factor): int
{
return $number * $factor;
}
$double = multiply(?, 2);
var_dump($double);
echo $double(15);The variable $double contains a Closure.
When $double(15) is executed, PHP passes 15 as the missing first argument and uses 2 as the predefined second argument.
The output is:
object(Closure)
30The resulting Closure retains the parameter names, type declarations, pass-by-reference behavior, and return type information from the original function.
This is useful because developers do not need to manually recreate the original function signature when preparing reusable callbacks.
Using Partial Function Application With array_map()
One of the most practical use cases is working with PHP array functions.
Developers often use array_map() to transform values in an array. When a transformation requires predefined arguments, an anonymous function is commonly used.
Consider the following example:
<?php
$products = [
"laptop",
"keyboard",
"mouse"
];
$formattedProducts = array_map(
fn($product) => strtoupper($product),
$products
);
print_r($formattedProducts);This works, but the arrow function is unnecessary because strtoupper() already provides the required functionality.
With PHP 8.6, developers can use Partial Function Application:
<?php
$products = [
"laptop",
"keyboard",
"mouse"
];
$formattedProducts = array_map(
strtoupper(...),
$products
);
print_r($formattedProducts);This example uses the existing first-class callable syntax because strtoupper() requires only one argument.
Partial Function Application becomes particularly useful when an existing function requires additional configuration.
For example:
<?php
$names = [
"john",
"sarah",
"michael"
];
$formattedNames = array_map(
str_pad(
?,
12,
"*",
STR_PAD_BOTH
),
$names
);
print_r($formattedNames);Here, the padding configuration is predefined, while the actual string is supplied by array_map().
This removes the need to create a separate callback for a simple transformation.
Using Partial Function Application With array_filter()
Another useful scenario is filtering arrays based on predefined conditions.
Suppose an application maintains a list of software versions and needs to identify versions that meet a minimum requirement.
Before PHP 8.6, developers could write:
<?php
$versions = [
"8.4",
"8.5",
"8.6",
"8.7",
"9.0"
];
$minimumVersion = "8.6";
$supportedVersions = array_filter(
$versions,
fn($version) => version_compare(
$version,
$minimumVersion,
">="
)
);
print_r($supportedVersions);With Partial Function Application, the same operation becomes:
<?php
$versions = [
"8.4",
"8.5",
"8.6",
"8.7",
"9.0"
];
$supportedVersions = array_filter(
$versions,
version_compare(?, "8.6", ">=")
);
print_r($supportedVersions);The first argument remains open, while the minimum version and comparison operator are predefined.
PHP creates a Closure that accepts the version being evaluated by array_filter().
This is a good example of how Partial Function Application can simplify callback-heavy code.
Using Named Arguments With Partial Function Application
PHP 8.6 also supports named placeholders.
This is particularly useful when a function contains multiple parameters and developers want to control the order of arguments supplied later.
Consider the following example:
<?php
function createUser(
string $name,
string $role,
bool $active = true
): array {
return [
"name" => $name,
"role" => $role,
"active" => $active
];
}
$createAdministrator = createUser(
name: ?,
role: "Administrator"
);
$user = $createAdministrator(
name: "John"
);
print_r($user);In this example, the role is predefined, while the name is supplied later.
Named placeholders also make it possible to reorder the arguments expected by the resulting Closure.
For example:
<?php
function createAccount(
string $name,
string $email,
string $role
): array {
return compact("name", "email", "role");
}
$accountFactory = createAccount(
email: ?,
name: ?,
role: "Developer"
);
$account = $accountFactory(
"[email protected]",
"John"
);
print_r($account);
Join the conversation! Your thoughts help the community grow.