PHP 8.6 SortDirection: cleaner, safer sorting APIs for modern PHP
PHP 8.6 introduces SortDirection, a small but useful step toward clearer sorting APIs. Here’s what it changes, why it matters, and how to use it in practice.
PHP has always offered multiple ways to sort arrays and data structures, but the APIs have not always been equally expressive. With PHP 8.6, the new SortDirection enum improves readability and type safety when you need to specify ascending or descending order.
This is a small language addition, but it fits a broader trend in modern PHP: moving from loosely typed flags and magic integers toward explicit, self-documenting APIs.
Why SortDirection matters
Traditionally, PHP sorting functions have relied on flags such as SORT_ASC and SORT_DESC, or on custom comparison callbacks. While familiar, these constants are still just integers under the hood, which makes code less explicit than it could be.
SortDirection gives developers a clearer way to express intent:
SortDirection::AscendingSortDirection::Descending
That means code becomes easier to read, easier to refactor, and less prone to accidental misuse.
A quick look at the new enum
In PHP 8.6, you can work with the enum directly instead of passing raw integer flags in places where the API supports it.
Example: using SortDirection in your code
use SortDirection;
function sortUsers(array $users, SortDirection $direction): array
{
usort($users, function (array $a, array $b) use ($direction): int {
$comparison = $a['name'] <=> $b['name'];
return match ($direction) {
SortDirection::Ascending => $comparison,
SortDirection::Descending => -$comparison,
};
});
return $users;
}
$users = [
['name' => 'Zoe'],
['name' => 'Anna'],
['name' => 'Mike'],
];
print_r(sortUsers($users, SortDirection::Ascending));
print_r(sortUsers($users, SortDirection::Descending));
This example shows one of the main benefits of the new enum: the direction is part of the type system, not just a convention.
Comparing with older approaches
Before SortDirection, you might have written code like this:
function sortUsers(array $users, int $direction): array
{
usort($users, function (array $a, array $b) use ($direction): int {
$comparison = $a['name'] <=> $b['name'];
if ($direction === SORT_DESC) {
return -$comparison;
}
return $comparison;
});
return $users;
}
This works, but it has a few drawbacks:
int $directiondoes not communicate meaning as well as an enumany integer can be passed in accidentally
the API depends on remembering which constant maps to which behavior
With SortDirection, the compiler and the runtime can help you express intent more clearly.
Practical use cases
You will most likely benefit from SortDirection in code that exposes sorting as a configurable feature, for example:
admin dashboards with user-selectable sort order
API endpoints with
sort=asc|descreusable collection helpers
sorting logic inside domain services
Example: mapping request input to SortDirection
use SortDirection;
function directionFromString(string $value): SortDirection
{
return strtolower($value) === 'desc'
? SortDirection::Descending
: SortDirection::Ascending;
}
$direction = directionFromString($_GET['direction'] ?? 'asc');
This keeps the boundary between user input and internal logic explicit. Parse strings once, then work with a strongly typed enum in the rest of the application.
A better fit for modern PHP codebases
The introduction of SortDirection may seem modest, but it aligns well with the direction PHP has taken in recent versions:
more enums
more explicit types
fewer ambiguous constants
clearer domain modeling
For teams maintaining larger Symfony or PHP applications, these changes reduce cognitive load. A small improvement in API clarity can save time every time a future developer revisits the code.
What this means for Symfony and application design
Even if your framework code does not directly expose SortDirection yet, you can still adopt the pattern in your own application code.
For example, when building a Symfony controller or service that handles sorting, you can:
accept a string from the request
validate and normalize it
convert it to
SortDirectionuse the enum in your domain or service layer
That separation helps keep controllers thin and business logic readable.
use SortDirection;
final class UserSorter
{
public function sort(array $users, SortDirection $direction): array
{
usort($users, function (array $a, array $b) use ($direction): int {
$result = $a['createdAt'] <=> $b['createdAt'];
return $direction === SortDirection::Ascending ? $result : -$result;
});
return $users;
}
}
This kind of code is easy to test and straightforward to extend.
Things to keep in mind
A few practical notes when adopting the new enum:
check your minimum PHP version before using it
keep input parsing at the edge of the application
prefer enums in your domain code, even if the external interface still uses strings
use
matchor explicit branching when direction affects business logic
Conclusion
SortDirection is a small addition, but it reflects an important principle in modern PHP: make intent explicit.
If you are building maintainable applications, especially in Symfony-based codebases, adopting enums like this can make your APIs clearer and your code easier to reason about.
As PHP continues to evolve, these incremental improvements are often the ones that have the biggest day-to-day impact for development teams.