What is New in PHP 8.4
PHP 8.4 brings exciting updates to enhance developer productivity and performance. It introduces powerful features like property hooks, new DOM improvements, and array functions, alongside performance tweaks and bug fixes. This version also includes new tools for better code safety and flexibility, making it a valuable upgrade for web projects.
Property Hooks
Property hooks allow developers to define custom logic when getting or setting property values. This reduces boilerplate code by replacing traditional getters and setters with inline hooks.
class User {
private string $name;
public string $displayName {
get => strtoupper($this->name);
set => $this->name = strtolower($value);
}
}
These hooks support both simple and complex operations, improving object-oriented design.
New DOM Enhancements
The DOM extension now supports HTML5 with a new Dom\HTMLDocument class, built on the Lexbor library. This offers better parsing and compliance with modern standards, while keeping the old DOMDocument for compatibility.
$doc = Dom\HTMLDocument::createFromString('<!DOCTYPE html><html><body>Hello</body></html>');
echo $doc->documentElement->tagName; // Outputs: html
This update fixes long-standing issues and adds convenient methods for handling HTML documents.
Array Functions
New array utilities simplify common tasks. Functions like array_find, array_find_key, array_any, and array_all help search and evaluate array elements efficiently.
$numbers = [1, 2, 3, 4];
$result = array_find($numbers, fn($value) => $value > 2);
echo $result; // Outputs: 3
These tools make array handling more intuitive and powerful.
Class Instantiation Improvements
You can now chain methods or access properties directly after creating an object without extra parentheses, reducing syntax clutter.
$class = new ReflectionClass(User::class)->getName();
echo $class; // Outputs: User
This change enhances readability, especially in complex method chains.
Asymmetric Visibility
Properties can now have different visibility for reading and writing, offering more control over access levels.
class Example {
public read private write string $data;
}
This feature supports advanced encapsulation strategies.
Additional Features
mb_trim,mb_ltrim, andmb_rtrimfor multi-byte string trimming.bcdivmodinBCMathfor division and modulus in one call.CURL_HTTP_VERSION_3andCURL_HTTP_VERSION_3ONLYfor HTTP/3 support.#[\Deprecated]attribute to mark outdated code.- New
DateTimemethods likecreateFromTimestampfor easier time handling.
Deprecations and Changes
Some features are marked for removal in future versions:
| Deprecated Feature | Details |
|---|---|
| Implicit nullable types | Requires explicit ?type syntax, e.g., ?string $value = null. |
| GET/POST session tracking | Disabled by default; use cookies instead. |
Performance and Bug Fixes
PHP 8.4 includes optimizations in the JIT compiler and fixes for memory leaks and security issues. These improvements ensure better stability and speed for web applications.
Why Upgrade to PHP 8.4
With its new tools and enhancements, PHP 8.4 offers a modern approach to web development. It provides better performance, cleaner code, and robust features, making it a smart choice for developers building secure and efficient websites.