252
What's New in PHP 8.4: Key Enhancements and Updates
28 Nov, 2024
8 min read
Table of Content
PHP 8.4 introduces a range of exciting features and updates designed to enhance the way developers work with the language. This release focuses on making coding more efficient, secure, and intuitive.
With additions like Property Hooks, Auto-Capturing Closures, and Asymmetric Visibility, developers can enjoy cleaner and more flexible code. New array functions also simplify common tasks, saving time and reducing boilerplate.
Alongside these features, PHP 8.4 brings performance improvements that help your applications run faster.
Whether you’re a PHP expert or just starting out, these updates will make your coding experience smoother and more enjoyable. If you’re looking to leverage the full potential of PHP 8.4 for your project, hire a PHP developer to ensure your application is built with the latest features and best practices.
With PHP 8.4, the evolution of one of the most popular server-side scripting languages continues, bringing a variety of tools and refinements to make developers’ lives easier. This release is packed with game-changing features that simplify coding and boost efficiency. Let’s explore all these updates in detail:
Property Hooks in PHP introduce a powerful way to manage class properties by allowing developers to attach specific behaviors when accessing or modifying them. This feature, introduced after read-only properties in PHP 8, provides hooks for “get” and “set” operations.
These hooks are essentially small callables that execute custom logic when a property is retrieved or updated, offering a clean way to validate or process values dynamically.
Unlike traditional methods that relied on boilerplate getters and setters or magic methods like __get and __set, property hooks streamline the process while maintaining clear and efficient code.
This new approach is especially useful in scenarios where properties must remain mutable but enforce certain rules or transformations. By reducing the need for redundant code, Property Hooks improves both the maintainability and readability of PHP applications.
Example:
class User implements Named
{
private bool $isModified = false;
public function __construct(private string $first, private string $last) {}
public string $fullName {
// Override the “read” action with arbitrary logic.
get => $this->first . ” ” . $this->last;
// Override the “write” action with arbitrary logic.
set {
[$this->first, $this->last] = explode(‘ ‘, $value, 2);
$this->isModified = true;
}
}
}
The PHP 8.4 release isn’t just about enhancements to its object-oriented features; it also brings in several handy new array functions. These functions are designed to streamline the process of evaluating array values based on specific conditions. Here’s what they offer:
Each function takes two parameters: the array to examine and a callback function that defines the condition. The callback is applied to each array element in sequence. If the condition is met, the function halts and returns the result immediately, except for array_all(), which continues until every element has been checked. Only if all elements pass the test does it return true.
These additions aim to make array operations more concise and expressive.
Example:
$numbers = [1, 2, 3, 4, 5];
$found = array_find($numbers, fn($value) => $value > 3);
echo $found; // Output: 4
$foundKey = array_find_key($numbers, fn($value) => $value > 3);
echo $foundKey; // Output: 3
$anyEven = array_any($numbers, fn($value) => $value % 2 === 0);
echo $anyEven ? ‘Yes’ : ‘No’; // Output: Yes
$allPositive = array_all($numbers, fn($value) => $value > 0);
echo $allPositive ? ‘Yes’ : ‘No’;
In PHP, developers frequently create objects and immediately call methods or access properties on them. Before PHP 8.4, achieving this required enclosing the instantiation in parentheses, sometimes resulting in unnecessarily verbose or cluttered code.
Example:
<?php
$session = (new Session())->start()->set(‘user’, ‘John Doe’);
?>
PHP 8.4 introduces a new feature called asymmetric property visibility, which allows developers to set different access levels for reading and writing a property. This allows you to make a property publicly readable while restricting its modification to within the class or its child classes, providing better control over data.
Example:
In the following example, the $name property is accessible for reading by anyone, but it can only be modified inside the class itself or by its subclasses:
<?php
class User {
public private(set) string $name;
public function __construct(string $name) {
$this->name = $name; // Allowed since it’s within the class
}
public function changeName(string $newName) {
$this->name = $newName; // Also allowed within the class
}
}
$user = new User(“Alice”);
echo $user->name; // Outputs: Alice (public read access)
$user->name = “Bob”; // Error: Cannot modify due to private(set)
?>
PHP has made a significant leap in web development by introducing the DOM\HTMLDocument class, providing native support for HTML5 parsing and serialization. This update resolves long-standing issues caused by PHP’s reliance on \DOMDocument, which uses libxml2 and is limited to HTML4.01. Previously, developers faced errors and malformed structures when working with HTML5 content due to outdated parsing capabilities.
Thanks to Niels Dossche’s implementation, the new DOM\HTMLDocument class integrates seamlessly into PHP’s DOM extension.
It uses Lexbor, a high-performance and standard-compliant HTML5 parser, to accurately handle HTML5’s broader tag set and parsing rules. The integration fixes issues like handling semantic elements and JavaScript embedded in HTML, while ensuring compatibility with older HTML4 documents.
To avoid disrupting existing workflows, the \DOMDocument class remains unchanged, with the new DOM\HTMLDocument class dedicated to HTML5. This enhancement modernizes PHP’s DOM extension, aligning it with current web standards and making it more robust for modern web development tasks.
If you’re looking for PHP web development services, this update ensures that developers can now leverage PHP’s full potential for handling contemporary web content.
PHP 8.4 introduces several updates, including the deprecation of implicit nullable types and GET/POST session handling. Here’s an overview of these changes and how they may affect your applications.
Implicit Nullable Types
Previously, you could create nullable types implicitly by assigning null as a default value:
function foo(T1 $a, T2 $b = null, T3 $c) {}
While PHP later introduced explicit nullable types:
function bar(T1 $a, ?T2 $b = null, T3 $c) {}
Or union types:
function test(T1 $a, T2|null $b = null, T3 $c) {}
The implicit approach remained valid. However, starting with PHP 8.4, this implicit declaration is deprecated. Developers must now use explicit nullable types or union types that include null.
Deprecation of GET/POST Sessions
Tracking user states is often done via cookies, which is recommended. However, PHP also allowed session handling through GET and POST parameters using the session.use_only_cookies and session.use_trans_sid settings.
By default, session.use_only_cookies is enabled, and session.use_trans_sid is disabled. These settings were meant to support browsers that previously lacked cookie support. With PHP 8.4, altering these settings will trigger a deprecation warning. In PHP 9, they will be entirely removed.
Other Deprecations
In addition to the above, PHP 8.4 includes several other deprecations, some resulting from recent RFCs. These changes continue PHP’s focus on modernizing and streamlining functionality, making reviewing and updating legacy code essential.
Adapting to these updates will help ensure your applications remain compatible with future PHP versions.
Here are some standout features that users can look forward to:
Release Date |
Milestone |
July 04, 2024 |
Alpha Release 1 |
July 18, 2024 |
Alpha Release 2 |
August 01, 2024 |
Alpha Release 3 (Skipped) |
August 1, 2024 |
Alpha Release 4 |
August 13, 2024 |
Feature Freeze |
August 15, 2024 |
Beta Release 1 (Skipped) |
August 15, 2024 |
Beta Release 2 (Skipped) |
August 15, 2024 |
Beta Release 3 |
August 29, 2024 |
Beta Release 4 |
September 12, 2024 |
Beta Release 5 |
September 26, 2024 |
Release Candidate 1 |
October 10, 2024 |
Release Candidate 2 |
October 24, 2024 |
Release Candidate 3 |
November 7, 2024 |
Release Candidate 4 |
November 21, 2024 |
General Availability Release |
The release of PHP 8.4 marks a significant milestone in PHP’s evolution. With enhanced features to boost security, performance, and overall developer experience, this update empowers developers to build faster and more resilient applications.
While PHP 8.4 introduces exciting new features, it also builds upon the foundation laid by PHP 8.3, continuing to refine and improve upon the PHP 8.3 features.
By addressing the needs of modern developers and businesses, PHP 8.4 underscores the language’s continued importance in web development and its commitment to staying relevant in an ever-evolving tech landscape.
Development
ERP
52
By Devik Gondaliya
03 Dec, 2024
Development
Ecommerce
Shopify
166
By Devik Gondaliya
26 Nov, 2024
News and Updates
300
By Biztech
22 Nov, 2024