Skip to content
PHP News
Search
Implemented PHP 8.6

enum SortDirection

Adds a global SortDirection enum with Ascending and Descending cases as a shared, type-safe way to express sort order.

Implement the SortDirection enum as outlined in the RFC?

Primary vote · 2/3 majority

22 Yes 2 No 2 abstain 92% approval

This poll has closed.

It passed 22 to 2, with 2 abstaining, against the two-thirds majority it needed. Voting closed on April 1, 2026. It has been merged into php-src, with PHP 8.6 as the target.

Summary

PHP has no single, type-safe way to say "sort ascending" or "sort descending." This RFC from Tim Düsterhus adds a small built-in enum, SortDirection, that PHP itself, frameworks and your own code can all share.

The problem

Today there are many ways to express the same thing:

  • SORT_ASC and SORT_DESC, which only work with array_multisort()
  • SCANDIR_SORT_ASCENDING and SCANDIR_SORT_DESCENDING, which only work with scandir()
  • Plain 'ASC' and 'DESC' strings
  • bool $ascending flags
  • Separate functions, like sort() and rsort()

Many frameworks and libraries end up defining their own type for this. Doctrine added its own Order enum only two years ago.

What it adds

The enum lives in the global namespace:

enum SortDirection {
    case Ascending;
    case Descending;
}

You could use it in a query builder like this:

$query->orderBy('created_at', SortDirection::Descending);

It's a pure enum, so its cases have no string or int backing value. The RFC says there's no single correct value, since SQL, API query strings and other contexts each want something different. You can map it yourself with a match().

It's also straightforward to polyfill for older PHP versions. A library can accept the enum alongside its old string or int values using a union type. Later on, PHP's own functions, like scandir(), could accept it too, and that kind of change wouldn't need an RFC.

What it means for existing code

You can no longer declare your own SortDirection class or enum in the global namespace. A GitHub search found 178 results named SortDirection, counting both namespaced and global ones, and the namespaced ones are unaffected. The RFC notes that adding new global names isn't considered a break under PHP's policy.