PHP 8.6's Tiny Standard Library Additions That Unify a Fragmented Ecosystem
PHP 8.6 introduces the SortDirection enum and Time\Duration class to its standard library, replacing over 150 incompatible userland implementations and inconsistent time representations with a shared vocabulary that enables seamless interoperability across the ecosystem and lays groundwork for a modern date/time API.
Every PHP project — whether built on a full-stack framework, a micro-service stack of independent libraries, or a CMS plugin — shares a common foundation: the PHP standard library. Functions like str_replace() and array_map(), and classes like DateTimeImmutable or the new Uri\Rfc3986\Uri in PHP 8.5, provide a consistent "lingua franca" that lets domain-specific libraries integrate with frameworks and lets developers move knowledge across the ecosystem.
Everyone Is Reinventing the Wheel
When common concepts such as "elapsed time measurement" or "multi-direction sorting" lack built-in types, almost every project creates its own solution. Individually each design makes sense, but integrating them requires glue code at every library boundary, forcing developers to relearn each library's "dialect."
PHP's own standard library is no exception. For representing a time span it uses seconds ( sleep()), milliseconds ( CURLOPT_TIMEOUT_MS), microseconds ( usleep()), nanoseconds ( hrtime()), integer pairs of seconds and fractional seconds ( stream_set_timeout()), and floating-point seconds ( microtime(true)).
For sort direction the situation is equally chaotic: integer constants like SORT_ASC and SCANDIR_SORT_ASCENDING work only with specific functions and carry no type constraints; array-sorting functions differ only in direction; userland libraries use strings like 'ASC', booleans, or their own enums. A GitHub search for SortDirection returns over 150 distinct, incompatible implementations, not counting differently named enums such as Doctrine's Order.
Two New Dictionary Entries
SortDirectionis a minimal enum with two cases — Ascending and Descending — and no attached behavior. Its entire definition fits in four lines:
enum SortDirection {
case Ascending;
case Descending;
}Trivial to implement in a single library, but when every library maintains its own version, cross-library usage demands complex mapping "babelfishes." With SortDirection guaranteed natively in PHP 8.6, libraries can use it directly in public APIs, converting only when serializing for SQL or HTTP.
$query->orderBy('created_at', SortDirection::Descending);Time\Durationrepresents a stopwatch-measured time span from 1 nanosecond to over 100 years with nanosecond precision throughout. Named constructors make the unit explicit — developers no longer guess whether a $timeout parameter expects seconds, milliseconds, or something else. Reading the value uses ->seconds, ->nanoseconds, and an explicit ->negative flag, eliminating "magic numbers." The new polling API introduced as part of the Streams Project in PHP 8.6 is the first native API to accept this type:
$events = $pollContext->wait(
Time\Duration::fromMilliseconds(250)
);A Foundational Building Block
Time\Durationis useful on its own, but it is only the first brick of a brand-new date/time API built on modern PHP capabilities. The next piece — Time\Instant and Time\Clock — is already under discussion. Time\Instant represents a point in time; Time\Clock provides an interface to ease unit testing of time-dependent logic. The distance between two Time\Instant objects is a Time\Duration; shifting an instant also takes a Time\Duration. They fit together like precision gears.
The Standard Library Deserves This Investment
A four-line enum and a class with three properties and a handful of methods might seem too small to justify a lengthy RFC process on PHP Internals — after all, anyone could write them in half an afternoon without coordination. Yet that is precisely why they yield the highest long-term return: they give the entire ecosystem a shared standard foundation, replacing a multitude of custom implementations, rather than merely rewriting existing code.
The need for these types already exists objectively. The community has produced 150+ distinct four-line enums and multiple userland libraries and extensions for stopwatch timing. All are good solutions to the same universal problem, yet they are mutually incompatible. A single unified design discussion on PHP Internals, despite higher upfront cost, permanently replaces all that fragmented duplication with a common baseline.
Planning for Decades Ahead
Deep discussion is crucial because getting the design right matters far more for the standard library than for any userland package. PHP's strong backward-compatibility commitment means there is virtually no "second chance" to rewrite. Once a feature hits a stable release, even the tiniest breaking change affects thousands of users.
Namespaces — introduced after most of the current standard library existed — provide a rare "do-over" opportunity. New replacement APIs can live in a fresh, empty namespace without disturbing the old APIs that developers have relied on for years. Users can migrate at their own pace. This approach has already succeeded with the Random extension ( Random\* replacing mt_rand(), shuffle()), the DOM extension ( Dom\* replacing DOM*), and the URI extension ( Uri\* replacing parse_url()).
To maximize this opportunity, these RFCs deliberately adopt a "small, fast steps" strategy while clearly outlining future scope, enabling long-range planning without over-committing. Internal consistency means today's decisions constrain tomorrow's; a small, well-designed, future-proof API is easy to extend, while a slightly flawed large API becomes a decades-long burden.
The Time\Duration RFC demonstrates this principle in practice. ext/date maintainer and PHP Foundation core developer Derick Rethans and the author wanted the new polling API to accept a Time\Duration timeout instead of separate $timeoutSeconds and $timeoutMicroseconds parameters before PHP 8.6's feature freeze locked the signature. From the first RFC proposal they had roughly two months. An inelegant single-method API is a local problem; replacing it with a poorly designed reusable building block fixes the small issue but creates a larger hazard. Therefore the proposal was strictly limited to:
The core parts the authors were confident about, adhering to a precautionary principle.
Features that future functionality depends on and that cannot be added later without breaking existing expectations (e.g., support for negative durations).
They explicitly asked the Internals community to focus review on core substantive issues, leaving optional conveniences for future planning. The result is a new class expressing a universal concept — immediately useful even if future extensions slip, while leaving ample room for Time\Instant and subsequent evolution.
Making This Effort Sustainable
None of this work is free. Building features that cohere the ecosystem requires RFC authors to research, design, and drive discussions; reviewers to invest time reading RFCs and contributing insights from production experience, sometimes debating names for multiple rounds until consensus; and implementers to write, review, and maintain code for years.
For transparency, the author works at Tideways — a PHP Foundation founding member and silver sponsor — which sponsors part of the author's time on SortDirection, Time\Duration, and the ongoing Time\Instant RFC. The PHP Foundation funds developers including co-author Derick to work on such efforts, a key reason the standard library has continued to improve in recent years.
Ways to help:
Try it : Test pre-release PHP versions, especially new features like Time\Duration, and report any issues so the final release makes a good first impression.
Accept it : Widen your library APIs to accept the new types. Union types let you do this without breaking changes, giving users clearer semantics while keeping trusted third-party libraries working.
Shape it : Participate in RFC discussions, such as the one for Time\Instant. Every perspective adds value.
Fund it : If you cannot contribute time, fund others — e.g., persuade your employer to sponsor the PHP Foundation. Every investment in PHP itself returns multiplied developer-experience gains.
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.
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.
