jonas.splinter
Blog

What I actually used from PHP 8.4 and 8.5, and what I skipped


A desk where a stack of cards is checked with a magnifying glass; most cards go in a bin, a few go in a small tray.

I had AI agents audit two Laravel apps and a WordPress theme for new PHP features. Of 51 suggestions, 9 survived review. What I adopted, what I skipped and why.

I moved two Laravel apps and a WordPress theme onto PHP 8.5 and then asked: am I actually using what the last two releases added? I had AI agents sweep every file for features from PHP 8.4 and 8.5, and a second, sceptical agent re-check each suggestion against the real code. Of 51 suggestions, 9 survived. This post covers what I adopted, what I threw out and the one lesson that mattered most: when you parse a URL to check it, fetch exactly the string you checked.

Start with what each part can run

Before any rewrite, I mapped which runtime executes which code. That map decided more than any feature list did. My web requests already ran the new release, but two other paths did not. A command-line tool still ran the older one, and a deploy script built caches with it too.

The rule that follows is simple. A new function can often ship early through a polyfill package. New syntax cannot: the pipe operator |> or clone() with properties is a parse error on an older runtime, and that error hits the whole file, not one line. So every suggestion got a label: can ship today, or blocked until a specific path moves. I then moved the deploy script and the remaining command-line callers first, and let the dependency manager enforce it by raising the required version in each app. After that, an older runtime refuses to start the app at all instead of failing halfway.

Most suggestions were churn

The sweep produced plenty of ideas that looked modern and changed nothing for a user. Three groups went straight back out:

  • #[\NoDiscard] is meant for functions whose return value must be checked. Most proposed targets were predicates or functions that already return nothing.
  • Property hooks clash with an ORM that builds attributes through magic methods. Neat in a plain class, a trap in a model.
  • The pipe operator for two-step chains. Readable, but blocked on part of my stack, and it fixes nothing.

What stayed was smaller and real. array_any() and array_find() replaced eight loops that set a flag and broke out. array_first() and array_last() replaced end() and reset(), which move an internal pointer and return false on an empty array. mb_trim() replaced trim() on names and mail subjects, where a pasted non-breaking space survived the old trim. I also deleted a compatibility class for PHP 7 that could never load any more.

The URI parser

Check the string you fetch

PHP 8.5 ships a URL parser that follows the WHATWG standard browsers use, Uri\WhatWg\Url. The obvious use is the code that guards outbound requests: parse the URL, check the host, resolve it, refuse private addresses. Those checks used parse_url(), which is known to read some odd URLs differently from browsers.

So I tested how the HTTP client itself reads the tricky cases, before swapping anything. Backslashes, @ signs and number-only hosts all came up. The client agreed with the old parser, not with the browser standard. Swapping only the checking parser would therefore have created the very mismatch I wanted to remove: the check would look at one host and the request would go to another.

The fix is to parse once and hand the client the result:

$url = Uri\WhatWg\Url::parse($input);
if ($url === null || ($url->getUsername() ?? '') !== '' || ($url->getPassword() ?? '') !== '') {
    throw new InvalidArgumentException('Invalid URL.');
}
$canonical = $url->withFragment(null)->toAsciiString();
// Resolve and check the host of $canonical, then request $canonical, pinned to the checked address.

Now the checked host and the contacted host are the same by construction. A bonus: number tricks like 2130706433 or 0x7f.1 become a plain dotted address before any check runs. A redirect Location gets resolved with $base->resolve($location), which replaced a hand-written resolver of twenty lines.

One review finding is worth passing on. Once addresses are canonical, IPv6 forms that embed an IPv4 address come out in hex. PHP's FILTER_FLAG_NO_PRIV_RANGE does not recognise all of those. Check IPv6 addresses by binary range instead, and refuse anything outside the global unicast block 2000::/3.

One helper, and a diff before switching

The same URL parsing happened in more than fifty places. Each one took the host of a submitted website and derived a stored domain from it. I replaced them all with one small helper on top of the new parser. Before switching any call site, I ran the old and the new logic side by side over every stored value, tens of thousands of URLs, read-only. Not one result changed. That diff is what made the change safe to ship: new input now parses the way a browser would, and existing data stays exactly as it was.

Faster command-line runs, with one caution

Scheduled jobs start PHP from scratch every minute and compile the same framework every time. The OPcache file cache keeps compiled code on disk between runs. In my measurements it roughly halved the start-up time of both a Laravel command and a WordPress command-line call.

The caution: do not share one cache directory between users. If a privileged job executes compiled code that the web user could write, that is a way to run code with more rights. I gave each user its own private directory, set by explicit flags on the busiest jobs only, instead of one global setting.

What I do not know yet

I measured start-up times on a few runs, not over weeks, so treat "roughly halved" as a sample. I also tried persistent connection sharing between requests, which is new in PHP 8.5. It saved little on my calls and needed invasive changes, so I left it out. That says more about my traffic than about the feature.

Frequently asked questions

Can I use PHP 8.5 functions while part of my stack still runs 8.4?

Often yes, through a polyfill package, as long as only functions are used. New syntax such as the pipe operator breaks the whole file on the older runtime, so it has to wait until every path that loads the file has moved.

Is parse_url unsafe?

Not by itself. The risk is two different parsers reading one string: one in your check and one in your HTTP client. Parse once, then pass that exact result along, whichever parser you choose.

Did array_find and friends change behaviour anywhere?

They can. array_find() returns null when nothing matches, where a hand-written loop may have used another default. array_first() returns null on an empty array, where reset() returned false. Keep the original default explicitly.

What to do first

  1. Map which runtime runs each path: web, scheduled jobs, deploy scripts and command-line tools.
  2. Raise the required PHP version in each app once every path runs the new release.
  3. In code that checks URLs before fetching them, request the exact canonical string you checked.
  4. Check IPv6 addresses by binary range, not only with filter flags.
  5. Try the OPcache file cache for scheduled jobs, with one private directory per user.
  6. Treat AI suggestions as a list to verify, and diff behaviour on real data before switching.

More posts about PHP, Laravel and security are on my blog.

Related posts

More to read on the same topics

Related updates

Short notes on the same topics