What’s New in PHP 8.5? Explore Pipelines, NoDiscard, Array Helpers & More
PHP 8.5 arrives with a suite of developer‑friendly enhancements—including a pipeline operator that eliminates temporary variables, a NoDiscard attribute that enforces return‑value usage, new array_first/array_last helpers, constant‑expression closures, global constant attributes, an exception‑handler getter, IntlListFormatter, CLI diff tools, a build‑date constant, and refined final property syntax—making everyday coding cleaner, safer, and more efficient.
PHP 8.5 is about to be released, and seasoned developers consider it one of the most exciting versions because the new features, though few, make daily work noticeably easier.
New Pipeline Operator: No More Temporary Variables
The pipeline operator, already seen in tweets and RFCs, allows chaining transformations without intermediate variables. For example, instead of trimming and lower‑casing an email address step by step, you can write:
"[email protected]" |> trim() |> strtolower() |> sendEmail();This left‑to‑right flow keeps the code concise.
NoDiscard Attribute: Stop Ignoring Return Values
The #[NoDiscard] attribute forces developers to use a function’s return value, emitting a warning if it is ignored. For instance:
#[NoDiscard]
function getName(): string {
return "Nuno";
}
getName(); // PHP warns: "Hey, you should use the return value!"To deliberately ignore the result, you must cast it to void.
Constant‑Expression Closures
PHP 8.5 now permits static closures in places that require compile‑time values, such as class constants, default property values, and attribute parameters:
class Example {
public const VALIDATOR = static function ($value) {
return !empty($value);
};
}This enables reusable validation logic directly in constants.
Array Helpers: array_first() and array_last()
Instead of using reset() or end() —which move the internal pointer—you can retrieve the first or last element without side effects:
$users = ["Adrian", "Maria", "Pedro"];
$first = array_first($users); // "Adrian"
$last = array_last($users); // "Pedro"These helpers are simple, intuitive, and do not disturb the array pointer.
Global Constant Attributes
Constants can now carry metadata via attributes, e.g.:
#[Deprecated("Use NEW_CONSTANT instead")]
const OLD_CONSTANT = 42;
echo OLD_CONSTANT; // 42 + deprecation warningThis is useful for frameworks that store configuration in constants.
Exception‑Handler Getter
A new get_exception_handler() function returns the currently registered exception handler, making it easier for frameworks like Laravel to introspect and modify global error handling:
set_exception_handler(fn($e) => echo "Caught: " . $e->getMessage());
$handler = get_exception_handler();
var_dump($handler);IntlListFormatter
The IntlListFormatter class formats localized lists automatically:
$formatter = new \Intl\IntlListFormatter('en', \Intl\IntlListFormatter::TYPE_AND);
echo $formatter->format(['Lisbon', 'Porto', 'Coimbra']); // "Lisbon, Porto, and Coimbra"CLI Diff Command
The new php -i --diff command shows which php.ini options differ from the defaults, simplifying configuration checks:
php -i --diff
memory_limit = -1
max_execution_time = 0Build‑Date Constant
PHP 8.5 introduces PHP_BUILD_DATE, which outputs the exact build timestamp of the binary, useful for verifying versions across multiple installations:
echo PHP_BUILD_DATE; // 2025‑09‑18 14:32:00Final Attribute for Properties
Properties can now be marked final directly in the constructor, preventing overrides in subclasses and improving code clarity:
class User {
public function __construct(
public final string $username,
public string $email
) {}
}CLI and Debugging Tweaks
Improved default error reporting with php -d overrides.
Deprecated‑function warnings are suppressed in CLI mode.
Various low‑level optimizations reduce memory usage and execution time.
These changes require no code modifications but make everyday PHP usage smoother, especially for framework developers and DevOps engineers.
Why Small Changes Matter
Even modest internal improvements and CLI adjustments reduce friction in the development workflow, allowing developers to spend more time writing useful code rather than tweaking configurations.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
21CTO
21CTO (21CTO.com) offers developers community, training, and services, making it your go‑to learning and service platform.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
