Take the State of PHP 2026 Survey →

enum SortDirection View as Markdown

Implemented (PHP 8.6)
Authors
Tim Düsterhus, timwolla@php.net
Date
2026-03-02
Version
1.0
Implementation
https://github.com/php/php-src/commit/32c1931f18109655bc074dd5cda3248b838de636
Source
wiki.php.net/rfc/sort_direction_enum

Vote

Implement the SortDirection enum as outlined in the RFC? closed
Yes
22
No
2
Abstain
2
26 voters
alcaeus Yes 2026-03-20 18:33
alexandredaubois Yes 2026-03-19 09:56
ashnazg Yes 2026-03-23 16:21
ayesh Yes 2026-03-19 09:57
beberlei Yes 2026-03-18 23:24
crell Abstain 2026-03-19 16:43
dams Yes 2026-03-23 09:07
daniels Yes 2026-03-21 18:57
derick Yes 2026-03-18 18:12
edorian Yes 2026-03-18 16:25
ericmann Yes 2026-03-19 03:39
galvao Yes 2026-03-19 04:36
girgias Yes 2026-03-19 18:17
iliaa Yes 2026-03-21 20:14
ilutov Yes 2026-03-20 23:23
malferov No 2026-04-01 12:00
marandall Yes 2026-03-23 11:40
mbeccati Abstain 2026-03-18 16:13
ocramius Yes 2026-03-18 15:53
petk Yes 2026-03-18 16:01
pmjones Yes 2026-03-20 19:17
pollita No 2026-03-25 19:37
ramsey Yes 2026-03-21 16:57
svpernova09 Yes 2026-03-23 15:30
theodorejb Yes 2026-03-19 13:00
timwolla Yes 2026-03-18 15:50

Discussions on the mailing list

[RFC] enum SortDirection
· 11 messages
[VOTE] enum SortDirection
· 4 messages

RFC text

Copied from wiki.php.net, last synchronized Oct 13, 2026 11:50 UTC

Introduction

PHP’s standard library has no type-safe way to express sort direction. Today we use the integer constants SORT_ASC and SORT_DESC (which only work with array_multisort()), SCANDIR_SORT_ASCENDING and SCANDIR_SORT_DESCENDING (which only work with scandir()), bare 'ASC' and 'DESC' strings, bool $ascending flags, or entirely separate functions such as sort() vs rsort().

Every major PHP framework and library ends up defining its own sort direction type. As an example, Doctrine introduced \Doctrine\Common\Collections\Order with Ascending and Descending cases as a replacement for its own now-deprecated string constants just two years ago.

This RFC proposes adding a single, always-available SortDirection enum to the global namespace:

<?php
 
enum SortDirection {
    case Ascending;
    case Descending;
}
 
?>

This is intentionally small. It is a universally useful, generic type representing a common use case that PHP itself, frameworks, and other userland code can all converge on.

Proposal

Add the following enum to ext/standard (in the global namespace):

<?php
 
enum SortDirection {
    case Ascending;
    case Descending;
}
 
?>

The enum is an unbacked enum, because there is no single obvious canonical serialization format. It is meant to represent a generic concept that is useful across a wide range of applications (e.g. SQL queries, query-string parameters in APIs, …) and these applications can trivially map the enum to an appropriate scalar value using match() statements.

Examples

SQL Query Builders:

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

Backward Incompatible Changes

The SortDirection class name in the global namespace is no longer available. A GitHub search for language:PHP /(enum|class) SortDirection/ returns 178 results (both namespaced and unnamespaced).

Adding new symbols is not considered a BC break with regard to PHP’s policy, the name is the obvious choice and there is not a significant usage in the wild, making this an acceptable impact.

Proposed PHP Version(s)

Next PHP 8.x (PHP 8.6).

RFC Impact

To the Ecosystem

APIs using the enum will benefit from improved type safety and discoverability.

It is also trivially polyfillable and supports forward compatibility when allowing the enum in addition to string / integer values by means of a union type. As such we expect quick adoption by the ecosystem.

To Existing Extensions

Existing extensions may consider supporting the enum in addition to existing values in “sort direction” parameters (e.g. by means of a union type).

To SAPIs

None.

Open Issues

None.

Future Scope

  • Leveraging the enum in PHP’s standard library (e.g. for scandir()’s second parameter). This can happen without an RFC when it's just widening the type to an enum.

Voting Choices

Primary Vote requiring a 2/3 majority to accept the RFC:

Vote: Implement the SortDirection enum as outlined in the RFC? — Yes: 22 · No: 2 · Abstain: 2 (closed)

Patches and Tests

Implementation

References

Rejected Features

None.

Changelog

  • 2026-03-02: Initial version