Skip to content

PHP 8.6 Ships with Partial Function Application, Clamp, and a Duration Class

PHP 8.6 has been released with new language and standard library features, including partial function application, a clamp function, and a Time\Duration class. Readonly properties can now also take default values.

2 min readBased on 1 source0 views
PROGRAMMING LANGUAGES

TL;DR

  • PHP 8.6 is out, with new features, changed defaults and deprecations.
  • Partial function application and a clamp function are among the new language additions.
  • Readonly properties can now have default values, and a new Time\Duration class covers stopwatch-style timing.

What arrived in PHP 8.6

PHP 8.6 is now available. Laravel News, which covers the release in detail, lists the new features, the changed defaults and the deprecations in the version. The release also has an official landing page on php.net, which Laravel News points readers to for the full announcement.

The headline additions fall into three groups. Two of them extend how functions and values are handled, and one adds a new class to the standard library.

Partial function application and clamp

Partial function application (PFA) lets a developer call a function with some of its arguments already filled in. The call returns a closure that accepts the remaining arguments. This is useful when a generic function needs to be specialised for a common case, such as passing a fixed configuration value into a callback without writing a wrapper by hand.

The new clamp function takes a value and two bounds. If the value lies inside the bounds it is returned unchanged. If it lies outside, the function returns the nearest bound. Developers who currently write min and max calls together to keep a number in range can express that in one call.

Time\Duration and readonly defaults

PHP 8.6 adds a Time\Duration class. Laravel News describes it as a final readonly class that represents a length of time with nanosecond precision, in the manner of a stopwatch. It provides factory methods for each unit, arithmetic methods, and comparison support. Code that currently measures elapsed time with raw numbers or ad hoc helpers may be able to switch to a single typed value.

The other change concerns readonly properties. Before PHP 8.6, a readonly property could not be given a default value, and the attempt was a compile-time error. Laravel News notes that this made it awkward to satisfy get-only interface properties introduced in PHP 8.4 with a fixed value. From 8.6, a readonly property can have a default, which removes that workaround.

Changed defaults and deprecations

The source material mentions changed defaults and deprecations in this release but does not list them. Teams planning an upgrade should read the full release notes on the official PHP site before assuming that existing code will run unchanged. Laravel News has also published a more detailed What's new in PHP 8.6 overview that covers the release in more depth.

Why it matters

For everyday PHP code, the most immediate gains are the clamp function and readonly defaults, both of which remove small amounts of boilerplate. Partial function application gives library authors another tool for building callbacks. The Time\Duration class may matter most to teams that have built their own timing utilities, since a standard type can replace them.

Upgrade work still has to start with the deprecation list. Projects that depend on behaviour changed in 8.6 should test in a staging environment before rolling the new version to production.

Sources

This article was written by Blozhik from the reporting below. Read the originals for full detail.

LaPHP 8.6 is released!Laravel News
Was this useful?

Keep reading

Discussion · 0

Sign in to join the discussion. Liking does not need an account.Sign inRegister
Loading comments…
blozhikDeveloper news from across the web, explained. Every story links to its original sources.