DEV Community

Cover image for PHP 8.6 Is Here: 5 New Features You'll Actually Use
Muiduzzaman Lipu for Refactorly

Posted on

PHP 8.6 Is Here: 5 New Features You'll Actually Use

PHP 8.6 is scheduled for general availability today, November 19, 2026. Its headline feature is one I've wanted for years: a ? that turns any function call into a closure.

I ran every example below in the official php:8.6-rc-cli Docker image (PHP 8.6.0RC7, the last release candidate). Each "before" snippet was run on PHP 8.5.12. These are the five changes I expect to use every week.

Video version:

1. Partial function application: ? builds the closure for you

Here's how you filter an array today:

$words = ['apple', 'kiwi', 'banana'];

array_filter($words, fn($w) => str_contains($w, 'a'));
Enter fullscreen mode Exit fullscreen mode

That arrow function exists only to fill in one argument. In PHP 8.6, you put a ? where that argument goes:

array_filter($words, str_contains(?, 'a'));
// [0 => 'apple', 2 => 'banana']
Enter fullscreen mode Exit fullscreen mode

With a ? in the argument list, PHP doesn't call str_contains. It returns a closure that waits for the missing argument.

It works with your own functions, with ... for "all remaining arguments", and with named arguments:

function greet($greeting, $name, $punct = '!') {
    return "$greeting, $name$punct";
}

$hi = greet('Hi', ?);
$hi('Ana');                 // "Hi, Ana!"

$yo = greet(?, 'Bob', ...);
$yo('Yo');                  // "Yo, Bob!"

$hey = greet(punct: '?', greeting: 'Hey', name: ?);
$hey('Cy');                 // "Hey, Cy?"
Enter fullscreen mode Exit fullscreen mode

The generated closure is a real function with real parameters. (new ReflectionFunction($hi))->getNumberOfParameters() returns 1.

2. Partials make the pipe operator usable

PHP 8.5 added the pipe operator |>. It feeds a value into a callable that takes one argument. For anything else, you had to wrap an arrow function in parentheses:

// PHP 8.5
$slug = '  Hello World PHP  '
    |> trim(...)
    |> strtolower(...)
    |> (fn($s) => str_replace(' ', '-', $s));
Enter fullscreen mode Exit fullscreen mode

In 8.6, the ? marks where the piped value goes:

// PHP 8.6
$slug = '  Hello World PHP  '
    |> trim(...)
    |> strtolower(...)
    |> str_replace(' ', '-', ?);
// "hello-world-php"
Enter fullscreen mode Exit fullscreen mode

These two features are clearly meant to be used together.

3. clamp() is built in

Keeping a number in a range used to be max(min(...)). And every time, you have to stop and think about which one goes outside:

$volume = max(0, min(100, $input));
Enter fullscreen mode Exit fullscreen mode

Now:

$volume = clamp($input, 0, 100);

clamp(150, 0, 100);   // 100
clamp(-5, 0, 100);    // 0
clamp(42, 0, 100);    // 42
Enter fullscreen mode Exit fullscreen mode

It uses normal PHP comparison, so floats and strings work too:

clamp(1.5, 0.0, 1.0);   // 1.0
clamp('m', 'a', 'f');   // 'f'
Enter fullscreen mode Exit fullscreen mode

It also checks its bounds. Swap min and max by mistake, and it throws instead of silently returning a wrong number:

clamp(5, 10, 0);
ValueError: clamp(): Argument #2 ($min) must be smaller than or equal to argument #3 ($max)
Enter fullscreen mode Exit fullscreen mode

4. readonly properties can have default values

On PHP 8.5, this is a compile error:

final class C { public readonly int $r = 3; }
// Fatal error: Readonly property C::$r cannot have default value
Enter fullscreen mode Exit fullscreen mode

So you wrote a constructor just to set a constant-ish value. In 8.6, the default goes on the property, and it's still readonly:

final class Config {
    public readonly int $retries = 3;
}

$c = new Config;
$c->retries;       // 3
$c->retries = 5;   // Error: Cannot modify readonly property Config::$retries
Enter fullscreen mode Exit fullscreen mode

5. Time\Duration instead of raw integers

Is $timeout = 30 seconds or milliseconds? The new immutable Time\Duration class puts the unit in the name:

use Time\Duration;

$timeout = Duration::fromSeconds(30);
$elapsed = Duration::fromMilliseconds(45250);

Duration::compare($elapsed, $timeout);   // 1 (longer)
$elapsed->seconds;                       // 45
$elapsed->nanoseconds;                   // 250000000

Duration::fromIso8601DurationString('PT1H30M')->seconds;  // 5400
Duration::fromMinutes(5)->multiplyBy(3)->seconds;         // 900
Enter fullscreen mode Exit fullscreen mode

It has named constructors for nanoseconds through hours, plus add, sub, multiplyBy, divideBy, negate and absolute. The constructor is private, so new Duration() throws, and you always say which unit you mean.

Before you upgrade: this is new syntax

On PHP 8.5, a partial application isn't a runtime error. It's a parse error, so the whole file won't load:

$ php -r '$f = str_contains(?, "a");'      # PHP 8.5.12
Parse error: syntax error, unexpected token "?"

$ php -r 'var_dump(function_exists("clamp"));'
bool(false)
Enter fullscreen mode Exit fullscreen mode

If your library still supports older PHP versions, wait before using ? in shared code. clamp() is easy to polyfill behind a function_exists check.

Recap

  • str_contains(?, 'a') returns a closure, no fn needed. ... fills the remaining arguments.
  • |> str_replace(' ', '-', ?) makes pipes clean.
  • clamp($x, 0, 100) replaces max(0, min(100, $x)), and it validates its bounds.
  • public readonly int $retries = 3; is finally allowed.
  • Time\Duration replaces "is this seconds or milliseconds?"

Which one are you using first? 👇


I post one tested coding tip every day on YouTube at Refactorly. The 30-second version of this one is here.

Top comments (0)