Categories: Outros

PHP 8.5: What’s New and Why It Matters

PHP continues to evolve, and version 8.5 brings a fresh wave of improvements that make writing modern, clean, and performant code easier than ever. Whether you’re a seasoned developer or just getting started, here’s everything you need to know about PHP 8.5.


What Is PHP 8.5?

PHP 8.5 is the latest major release in the PHP 8.x series, building on the strong foundation laid by PHP 8.0 through 8.4. It introduces new language features, performance enhancements, and deprecations that push PHP further into the modern programming landscape.


Key Features in PHP 8.5

1. Pipe Operator (|>)

The pipe operator allows chaining callables left-to-right, passing values smoothly through multiple functions — no messy intermediary variables or deeply nested calls.

Before PHP 8.5:

$result = trim(strtolower(htmlspecialchars($input)));
PHP

With the Pipe Operator:

$result = $input
    |> htmlspecialchars(...)
    |> strtolower(...)
    |> trim(...);
PHP

2. Built-in URI Extension

PHP 8.5 ships with a new always-available URI extension for securely parsing, normalizing, and modifying URLs — following RFC 3986 and WHATWG URL standards.

$uri = Uri\Uri::parse('https://example.com/path?query=hello#fragment');

echo $uri->getHost();   // example.com
echo $uri->getPath();   // /path
echo $uri->getQuery();  // query=hello
PHP

3. Clone With

You can now update properties during object cloning using the new clone() syntax with an associative array — perfect for readonly classes.

class Point {
    public function __construct(
        public readonly int $x,
        public readonly int $y,
    ) {}
}

$point = new Point(1, 2);
$moved = clone($point, ['x' => 10]);

echo $moved->x; // 10
echo $moved->y; // 2
PHP

4. #[\NoDiscard] Attribute

This attribute warns developers when a function’s return value isn’t used — preventing silent bugs in APIs where the return value matters.

#[\NoDiscard]
function findUser(int $id): ?User {
    return User::find($id);
}

// This will trigger a warning — return value ignored!
findUser(42);

// Correct usage:
$user = findUser(42);
PHP

5. array_first() and array_last()

Two new built-in array functions that return the first or last element of an array. Returns null for empty arrays — great to compose with the ?? operator.

$items = ['apple', 'banana', 'cherry'];

echo array_first($items); // apple
echo array_last($items);  // cherry

$empty = [];
echo array_first($empty) ?? 'No items'; // No items
PHP

6. Persistent cURL Share Handles

Handles created with curl_share_init_persistent() now persist across PHP requests, avoiding repeated connection initialization to the same hosts.

$share = curl_share_init_persistent(['cookie', 'ssl_session']);

$ch = curl_init('https://api.example.com/data');
curl_setopt($ch, CURLOPT_SHARE, $share);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch);
curl_close($ch);
// $share persists for the next request automatically
PHP

7. Closures in Constant Expressions

Static closures and first-class callables can now be used in constant expressions — including attribute parameters and default property values.

class Formatter {
    public const TRIM = trim(...);
    public const UPPER = strtoupper(...);
}

$process = Formatter::TRIM;
echo $process('  hello  '); // "hello"
PHP

8. Property Hooks (Expanded Support)

PHP 8.5 expands property hooks, giving developers more granular control over how class properties are read and written — reducing boilerplate significantly.

class User {
    public string $name {
        get => strtoupper($this->name);
        set(string $value) => $this->name = trim($value);
    }
}

$user = new User();
$user->name = '  BIG BOSS  ';
echo $user->name; // BIG BOSS
PHP

9. JIT Compiler Improvements

Under the hood, PHP 8.5 ships with further JIT (Just-In-Time) compiler improvements that deliver measurable speed boosts — especially for CPU-intensive applications and high-traffic web apps.


What’s Deprecated in PHP 8.5?

PHP 8.5 deprecates several legacy functions and behaviors. Key ones to watch:

  • shell_exec() behavior changes
  • array_key_exists() with non-array arguments
  • class_alias() restrictions
  • Magic methods __sleep(), __wakeup(), __serialize(), and __unserialize() updates
  • Implicit nullable type declarations

Running your project through a static analysis tool like PHPStan or Psalm is a smart first step before upgrading.


Should You Upgrade?

Yes — but plan it carefully. Here’s a quick checklist:

  1. Test your codebase against PHP 8.5 in a staging environment first
  2. Update your dependencies — check Composer packages for PHP 8.5 compatibility
  3. Review deprecation notices — fix them before they become breaking errors
  4. Leverage new features — refactor where it makes sense for cleaner, faster code

The Bottom Line

PHP 8.5 is a solid, developer-friendly release that rewards those who keep their codebases modern and well-maintained. From the pipe operator to async improvements and performance boosts, it’s a release worth embracing.

Whether you’re running a personal blog or a high-traffic web application, staying current with PHP means faster, safer, and more maintainable code — and that’s always worth the upgrade.


References & Further Reading

Karson Adam

Share
Published by
Karson Adam

Recent Posts

Guide to High Uptime Hosting Architecture

A practical guide to high uptime hosting architecture, covering redundancy, failover, DNS, databases, monitoring, and…

1 day ago

How to Fix Redirect Loop on Hosting Domain

Learn how to fix redirect loop on hosting domain setups by checking SSL, DNS, CMS,…

3 days ago

Best Web Hosting for Uptime Monitoring

Find the best web hosting for uptime monitoring with practical criteria on alerts, logs, regions,…

5 days ago

Dedicated Server Hosting: Who Actually Needs It

Dedicated server hosting gives you full server resources, tighter control, and steadier performance when shared…

1 week ago

How to Prevent Open Redirect Vulnerabilities

Learn how to prevent open redirect vulnerabilities with safe redirect patterns, validation rules, allowlists, and…

1 week ago

Best Web Hosting for Ecommerce Stores

Find the best web hosting for ecommerce stores based on speed, uptime, scaling, security, and…

2 weeks ago