Skip to main content

Cloudsoft Zone Build, deploy and scale with practical guides to PHP, Node.js, React, DevOps and AI automation. Explore the guides

Cloudsoft Zone
PHP

What's new in PHP 8.5: pipe operator, clone with, URI extension and more

PHP 8.5 adds the pipe operator, clone with for readonly objects, a standards-based URI extension, #[\NoDiscard] and array_first(). This explainer shows each feature with tested examples and lists the deprecations to fix.

Nilesh Kushvaha

Updated 8 min read

Cloudsoft Zone cover: What's new in PHP 8.5: pipe operator, clone with, URI extension and more
Image: Cloudsoft Zone
In this article

Part 2 of 7

Upgrading PHP and Laravel

All parts

Why to move, what changes, how to catch breakage, then the upgrades themselves and tuning afterwards.

  1. What's new in PHP 8.5: pipe operator, clone with, URI extension and more

    You are here · 8 min read

PHP 8.5 was released on 20 November 2025 and, as of October 2026, it is the newest stable branch of the language, with active support until 31 December 2027 and security fixes until 31 December 2029. If you are planning an upgrade from 8.2, 8.3 or 8.4, it is the version most teams should aim for.

What's new in PHP 8.5 is a small set of features that change how everyday code reads: the pipe operator, clone with, #[\NoDiscard] and array_first(), plus a standards-based URI extension and deprecations that prepare for PHP 9.

The release is not a rewrite. It adds a handful of language features that change how everyday code reads, a standards-based URL parser that fixes a long-standing weak spot, and a set of deprecations that prepare the ground for PHP 9. This article explains what each change does, shows short examples you can run on PHP 8.5, and points out where the new features are useful and where they are not.

All examples were checked against the PHP 8.5 release announcement and the PHP manual on php.net. For the complete list of changes, read the official migration guide.

The pipe operator (|>)

The pipe operator passes the value on its left into the single-parameter callable on its right and returns the result. Chained together, pipes let you read a sequence of transformations from top to bottom instead of from the innermost function call outwards.

PHP
<?php
// Before: read from the inside out
$slug = strtolower(str_replace(' ', '-', trim($title)));

// PHP 8.5: read from top to bottom
$slug = $title
    |> trim(...)
    |> (fn(string $s): string => str_replace(' ', '-', $s))
    |> strtolower(...);

Three details matter in practice:

  • The right-hand side must be a callable that takes one argument: a first-class callable such as trim(...), a closure, or an invokable object.
  • Arrow functions must be wrapped in parentheses, as in the example. That is the syntax shown in the release announcement and the manual.
  • Functions that need more than one argument, such as str_replace(), need a small closure. For long pipelines of this kind, a well-named helper function can still be clearer.

Use pipes where a value flows through several steps, such as normalising input, transforming arrays or building strings. Do not rewrite every nested call; two calls nested inside each other are often easier to read as they are.

Clone with: changing properties while cloning

Readonly classes are a good fit for value objects, but until now the "with-er" pattern (returning a modified copy) needed boilerplate. In PHP 8.5, clone() accepts an associative array of properties to change on the copy:

PHP
<?php
final readonly class Money
{
    public function __construct(
        public int $amountInCents,
        public string $currency,
    ) {}

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

$price = new Money(1999, 'USD');
$discounted = $price->withAmount(1499);

echo $price->amountInCents;      // 1999
echo $discounted->amountInCents; // 1499

The original object is unchanged. Call clone() with an array from inside the class, as above. Public readonly properties can only be written from the class's own scope, so clone($price, ['currency' => 'EUR']) written outside Money fails with an Error ("Cannot modify protected(set) readonly property"). Small with...() methods respect that rule and give you a place to validate the new value.

A built-in URI extension

For many years PHP developers used parse_url(), which does not follow any URL standard and handles malformed input inconsistently. PHP 8.5 adds an always-available URI extension with two immutable classes:

  • Uri\Rfc3986\Uri parses URIs according to RFC 3986 (powered by the uriparser library).
  • Uri\WhatWg\Url parses URLs according to the WHATWG URL standard used by web browsers (powered by the Lexbor library).
PHP
<?php
use Uri\Rfc3986\Uri;

$uri = Uri::parse('https://example.com/docs/guide?lang=en#install');

if ($uri === null) {
    throw new InvalidArgumentException('Not a valid URI.');
}

echo $uri->getHost();   // example.com
echo $uri->getPath();   // /docs/guide
echo $uri->getQuery();  // lang=en

$next = $uri->withPath('/docs/upgrade')->withFragment(null);
echo $next->toString(); // https://example.com/docs/upgrade?lang=en

The static parse() method returns null for invalid input, while the constructor throws an exception. Because the objects are immutable, every with...() method returns a new instance. Use the WHATWG class when you need to match what a browser would do with a user-supplied URL, for example when validating redirect targets, and the RFC 3986 class for general URI handling.

#[\NoDiscard]: warnings for ignored return values

Some functions are dangerous to call without using their result. A method that returns a new immutable object, or one that returns an error status instead of throwing, is easy to misuse. The new #[\NoDiscard] attribute makes PHP emit a warning when the return value is not used:

PHP
<?php
final readonly class Settings
{
    public function __construct(public bool $debug = false) {}

    #[\NoDiscard('the original Settings object is not modified')]
    public function withDebug(bool $debug): self
    {
        return clone($this, ['debug' => $debug]);
    }
}

$settings = new Settings();
$settings->withDebug(true);         // Warning: the result is discarded
$settings = $settings->withDebug(true); // Correct
(void) $settings->withDebug(false);    // Intentionally ignored, no warning

The (void) cast, added alongside the attribute, documents that a value is ignored on purpose. Library authors get the most from this attribute; application code benefits automatically when its dependencies adopt it.

array_first() and array_last()

PHP 7.3 added array_key_first() and array_key_last(), but getting the values still needed an extra step. PHP 8.5 adds array_first() and array_last(), which return the first or last value, or null for an empty array:

PHP
<?php
$events = ['signup', 'trial_started', 'subscribed'];

echo array_first($events); // signup
echo array_last($events);  // subscribed

$latest = array_last([]) ?? 'none'; // 'none'

Because the result is null for an empty array, these functions cannot tell you the difference between an empty array and an array whose last value is null. Check for [] first if that distinction matters.

What's new in PHP 8.5: smaller additions worth knowing

The release announcement lists a number of smaller improvements. The ones most likely to affect everyday work are:

FeatureWhy it helps
Backtraces for fatal errors, such as an exceeded maximum execution timeEasier debugging of timeouts in production logs
Static closures and first-class callables in constant expressionsCallables can be used as attribute arguments and default values
#[\Override] on propertiesPHP checks that a property really overrides a parent property
Asymmetric visibility for static propertiesFor example public private(set) static int $calls
final in constructor property promotionMark promoted properties final without a separate declaration
Closure::getCurrent()Simpler recursion inside anonymous functions
Persistent cURL share handles via curl_share_init_persistent()Reuse DNS and connection data across requests
partitioned option for setcookie()Supports partitioned (CHIPS) cookies
get_error_handler() and get_exception_handler()Inspect the current handlers without replacing them

On the operations side, OPcache is now always built into the PHP binary and always loaded. The opcache.enable and opcache.enable_cli settings still control whether it is used. A new opcache.file_cache_read_only setting supports a read-only file cache, which is useful in read-only Docker containers.

Light blue plush elephant toy with a php logo and a small star badge, on a white background
PHP 8.5 is supported with security fixes until 31 December 2029.

Deprecations and breaking changes

PHP 8.5 deprecates several older patterns. Deprecations do not break code today, but they appear in logs and will become errors in a future major version:

  • The backtick operator as an alias of shell_exec().
  • The non-canonical casts (boolean), (integer), (double) and (binary); use (bool), (int), (float) and (string).
  • Ending case statements with a semicolon instead of a colon.
  • Using null as an array offset or as the key in array_key_exists().
  • The __sleep() and __wakeup() magic methods are soft-deprecated in favour of __serialize() and __unserialize().

Some changes affect behaviour directly. The disable_classes INI setting has been removed. A warning is now emitted when a float, or a string that looks like a float, cannot be represented as an integer during an int cast, and when NAN is cast to another type. Destructuring a non-array value other than null now emits a warning. Using "array" or "callable" as an alias name in class_alias() is no longer possible. Read the backward-incompatible changes page of the migration guide for extension-specific changes, for example in PDO, Intl and MySQLi.

Should you upgrade now?

For most applications, yes. PHP 8.5 has been stable since November 2025 and has received regular patch releases; the latest at the time of writing is 8.5.11, a security release from 24 September 2026. Before upgrading:

  1. Confirm that your framework or CMS supports PHP 8.5 in its own documentation or composer.json.
  2. Add PHP 8.5 to your CI matrix and fix the deprecation notices it reports.
  3. Test on a staging server built from the same image you will deploy.

PHP 8.6 is at the release-candidate stage, with general availability planned for 19 November 2026 according to the PHP wiki. That is a reason to test it, not a reason to wait: the upgrade path from 8.5 to 8.6 is likely to be short.

Summary

PHP 8.5 makes everyday code easier to read with the pipe operator, makes immutable objects practical with clone-with, gives PHP a standards-based URL parser, and helps API authors prevent mistakes with #[\NoDiscard]. Start with the features that remove boilerplate in your own code, fix the deprecations your tests report, and use the official migration guide as your checklist.

Frequently asked questions

  • When was PHP 8.5 released and how long is it supported?

    PHP 8.5 was released on 20 November 2025. It has active support until 31 December 2027 and security support until 31 December 2029.

  • Can I use any function with the pipe operator?

    The right-hand side must be a callable that accepts one argument, such as trim(...) or a closure. Functions that need extra arguments must be wrapped in a closure, and arrow functions must be wrapped in parentheses.

  • What is the difference between Uri\Rfc3986\Uri and Uri\WhatWg\Url?

    The first parses URIs according to RFC 3986. The second follows the WHATWG URL standard used by browsers. Use the WHATWG class when you need browser-compatible handling of user-supplied URLs.

  • Does clone with work from outside a class?

    It works for properties the calling scope may write. Public readonly properties are only writable from inside the class, so expose a with...() method that calls clone($this, [...]).

Sources

  1. PHP 8.5 Release Announcement — The PHP Group, 2025-11-20
  2. Functional Operators (pipe operator) — PHP Manual
  3. The Uri\Rfc3986\Uri class — PHP Manual
  4. The NoDiscard attribute — PHP Manual
  5. Migrating from PHP 8.4.x to PHP 8.5.x: Backward Incompatible Changes — PHP Manual
  6. Supported Versions — The PHP Group
  7. PHP 8.6 release schedule (todo/php86) — PHP Wiki

Part 3 of 7: Upgrading PHP and Laravel

Previous part

Comments

No comments yet. Be the first to join the conversation.

Leave a comment

Every comment is read by a moderator before it appears. Your email address is never published; it is used only to contact you about this comment.

Never published. Used only to contact you about this comment.

Loading the security check…

Comments are moderated before publication.