PHP

PHP 8.5 features I tried: pipes, clone with and real URI parsing

5 min read

Pipes, clones and URIs

Hi folks,

PHP 8.5 came out on 20 November 2025, so it has just turned one year old. I am a bit ashamed to say I had read the release page back then and then… did nothing with it. My projects ran on 8.3 and 8.4, the TYPO3 sites were busy with v14 upgrades, and new syntax was simply not on the list.

This week I finally sat down and tried the features I was most curious about. Not in a project, just a single PHP file in a container, so nothing on my machine had to change. Here’s what I found.

How I tested

DDEV for this project still runs PHP 8.4, and I didn’t want to switch the whole project for a quick test. The official Docker image is enough:

docker run --rm -v "$PWD":/app -w /app php:8.5-cli php demo85.php

All the outputs below are from that run. When you want the real thing in a project, check first that your TYPO3 or WordPress version supports 8.5, and that all your Composer packages do too. composer why-not php 8.5 is a nice quick check.

The pipe operator

This is the one everybody talks about. Let’s say you make a slug from a title. Before, you wrote it inside out:

$slug = trim(preg_replace('/[^a-z0-9]+/', '-', strtolower(trim($title))), '-');

You read it from the middle to the outside, which my brain always does like a snail. With |> the value flows from top to bottom:

$title = '  Fluid 5 in TYPO3 v14: what changed?  ';

$slug = $title
    |> trim(...)
    |> strtolower(...)
    |> (fn(string $s): string => preg_replace('/[^a-z0-9]+/', '-', $s))
    |> (fn(string $s): string => trim($s, '-'));

var_dump($slug); // string(33) "fluid-5-in-typo3-v14-what-changed"

Each step takes exactly one argument, the result of the step before. That’s why trim(...) and strtolower(...) work directly as first-class callables, but preg_replace needs a small arrow function, because the value has to go into its third argument. Also note the brackets around the arrow functions. Without them, the parser gets confused.

Honestly, I like it for exactly this kind of string and array cleanup. I’m not going to rewrite whole service methods into pipes, though. When a step needs two or three extra arguments, a plain variable per step is still easier to read and to debug.

Clone with

This one makes me happy because readonly value objects were a bit painful. In 8.3 and 8.4, a withSomething() method on a readonly class needed either a full new self(...) with every property, or a __clone() trick. Now clone takes an array of properties to change:

final readonly class Money
{
    public function __construct(
        public int $cents,
        public string $currency,
    ) {}

    public function withCents(int $cents): self
    {
        return clone($this, ['cents' => $cents]);
    }
}

$price = new Money(1999, 'EUR');
$newPrice = $price->withCents(2499); // currency stays 'EUR'

It works from inside the class scope, where the readonly property may be set. I think this will clean up a lot of DTOs and settings objects in extensions. Less code, and you can’t forget a property when the class grows.

#[NoDiscard]

Have you ever called a method on an immutable object and forgot to use the result? I have, more than once, with PSR-7 requests. $request->withAttribute(...) on its own line does nothing. PHP 8.5 lets you mark a function so that ignoring its return value gives a warning:

#[NoDiscard('the new state is in the return value')]
function addTag(array $tags, string $tag): array
{
    return [...$tags, $tag];
}

$tags = ['PHP 8'];
addTag($tags, 'TYPO3');
// Warning: The return value of function addTag() should either be used
// or intentionally ignored by casting it as (void), the new state is in the return value

(void) addTag($tags, 'ignored on purpose'); // no warning

The (void) cast is how you say “yes, I really mean to ignore it”. I can see this on all the with*() methods of value objects. The warning appears at runtime, so you only see it when that line actually runs. Static analysis tools will pick it up sooner, once they support it.

array_first() and array_last()

Small, but so welcome. No more reset() and end() with their side effects on the internal pointer, and no $array[array_key_first($array)] gymnastics:

$events = ['2026-11-12' => 'outage', '2026-11-14' => 'restored'];

array_first($events); // 'outage'
array_last($events);  // 'restored'
array_first([]);      // null

Keys don’t matter, it gives you the value. For an empty array you get null, so if null can also be a real value in your array, you still need to check count() first.

The URI extension

This is the feature I think is most underrated. parse_url() has always been a bit wild. It follows no standard exactly, and with strange input it gives strange results, which is not great when the URL comes from a user. PHP 8.5 ships an always-enabled uri extension with two classes: one for RFC 3986 and one for the WHATWG URL standard that browsers use.

use UriRfc3986Uri;
use UriWhatWgUrl;

$uri = new Uri('https://hoja.in/blog/?p=1#top');
$uri->getHost();                         // 'hoja.in'
$uri->getQuery();                        // 'p=1'
$uri->withPath('/blog/feed/')->toString(); // 'https://hoja.in/blog/feed/?p=1#top'

Uri::parse('http://exa mple.com');       // null instead of an exception

$url = Url::parse('https://bücher.example/straße');
$url->getAsciiHost();   // 'xn--bcher-kva.example'
$url->getUnicodeHost(); // 'bücher.example'
$url->toAsciiString();  // 'https://xn--bcher-kva.example/stra%C3%9Fe'

Two things I noticed. The objects are immutable, with with*() methods like PSR-7. And parse() returns null on invalid input, while the constructor throws an exception, so you can pick whichever fits. The WHATWG class also handles internationalised domain names, which parse_url() never did.

Which one to use? My rule for now: if you validate or rewrite URLs that a browser will open, like redirects or links from editors, use UriWhatWgUrl, because it parses the way the browser does. For other protocols and identifiers, use the RFC 3986 class.

Smaller things worth knowing

  • Fatal errors, for example from max_execution_time, now come with a backtrace. That alone can save an hour of guessing.
  • Closures and first-class callables are allowed in constant expressions, so also in attribute arguments and default values.
  • #[Override] now works on properties too.
  • setcookie() and the session functions support partitioned cookies.

Will I upgrade?

Yes, but step by step. First the dev environment and CI, then one smaller site, then the rest. And I’ll wait to use the pipe operator and clone with arguments in shared extensions until every project that uses them runs on 8.5, because both are syntax errors on older PHP.

Which of these do you already use? Feedbacks are the champion’s breakfast, drop a comment.

Further reading

Leave a comment

Your email address will not be published.