November 21 2024 PHP 8.4 release brings new features and goodies to make your life as a developer and PHP user better and code better. PHP continues to evolve the language while keeping the speed and security strong with this release.
PHP 8.4 has something for everyone, regardless of your level of developer or PHP experience. Let’s see what’s new.
Table of Contents
1. Property Hooks: Define Property Management
One of the new features in PHP 8.4 is property hooks. This allows you to define getter and setter logic for properties without the boilerplate code. Inspired by Kotlin and Swift.
Example:
class User {
private string $first;
private string $last;
public string $fullName {
get => $this->first . " " . $this->last;
set {
[$this->first, $this->last] = explode(' ', $value, 2);
}
}
}This feature enhances code readability and maintainability, particularly for complex applications.
2. Asymmetric Property Visibility
The asymmetric visibility feature provides developers with more granular control over property access by allowing different levels of visibility for reading and writing.
Example:
class Book {
public function __construct(
public private(set) string $title,
public protected(set) string $author
) {}
}This feature is particularly useful for scenarios where data should only be modifiable under specific conditions.
3. Simplified Object Instantiation
In PHP 8.4, method chaining after object instantiation is now more concise, removing the need for additional parentheses.
Before:
$request = (new Request())->withMethod('GET')->withUri('/hello-world');Now:
$request = new Request()->withMethod('GET')->withUri('/hello-world');This enhancement streamlines code and improves developer productivity.
4. Enhanced Array Functions
PHP 8.4 introduces powerful array manipulation functions, allowing developers to perform operations more efficiently:
1. array_find()
This function locates the first element in an array that matches a given condition. If no element matches, it returns null.
Syntax:
array_find(array $array, callable $callback): mixedExample:
$numbers = [2, 4, 7, 8, 10];
$isOdd = fn($n) => $n % 2 !== 0;
$firstOdd = array_find($numbers, $isOdd);
echo $firstOdd; // Output: 72. array_find_key()
This function retrieves the key of the first element in an array that matches a given condition. If no element matches, it returns null.
Syntax:
array_find_key(array $array, callable $callback): mixedExample:
$numbers = [10 => 2, 20 => 4, 30 => 7, 40 => 8];
$isOdd = fn($n) => $n % 2 !== 0;
$firstOddKey = array_find_key($numbers, $isOdd);
echo $firstOddKey; // Output: 303. array_any()
This function checks if at least one element in an array satisfies a given condition. It returns true if the condition is met by any element, otherwise false.
Syntax:
array_any(array $array, callable $callback): boolExample:
$numbers = [2, 4, 6, 8, 9];
$isOdd = fn($n) => $n % 2 !== 0;
$hasOdd = array_any($numbers, $isOdd);
var_dump($hasOdd); // Output: bool(true)4. array_all()
This function checks if all elements in an array satisfy a given condition. It returns true if all elements meet the condition, otherwise false.
Syntax:
array_all(array $array, callable $callback): boolExample:
$numbers = [2, 4, 6, 8];
$isEven = fn($n) => $n % 2 === 0;
$allEven = array_all($numbers, $isEven);
var_dump($allEven); // Output: bool(true)5. Improved DateTime Handling
Managing DateTime objects is simpler with the new createFromTimestamp() method. It supports both integer and floating-point timestamps.
Example:
$dt = DateTimeImmutable::createFromTimestamp(1718337072.432);
echo $dt->format('Y-m-d h:i:s.u'); // 2024-06-14 03:51:12.4320006. New Multibyte String Functions
PHP 8.4 includes additional multibyte string functions, enabling better handling of UTF-8 text:
- mb_trim()
- mb_ltrim()
- mb_rtrim()
- mb_ucfirst()
- mb_lcfirst()
These functions enhance string manipulation in multilingual applications.
7. HTML5 Parser with Dom\HTMLDocument
The new \Dom\HTMLDocument class introduces native HTML5 parsing, providing developers with an efficient way to handle modern web content.
Example:
$doc = \Dom\HTMLDocument::createFromString($contents);This improvement ensures compatibility with contemporary HTML standards.
8. JIT Compiler Enhancements
PHP 8.4 includes performance optimizations for the Just-In-Time (JIT) compiler, making it more configurable and efficient. Disabling JIT is as simple as:
opcache.jit=disable
opcache.jit_buffer_size=64mDeprecations in PHP 8.4
To encourage better coding habits PHP 8.4 deprecates:
- Implicit nullable types
- E_STRICT constant
- GET/POST sessions
- old cryptographic functions like md5() and sha1()
These are for security and performance and to reduce legacy code issues.
Performance and Security
PHP 8.4 has big performance and memory improvements. Deprecated cryptographic functions are replaced with better alternatives to make your application more secure.
Upgrade to PHP 8.4
Follow these steps to make the upgrade smooth:
- Review Deprecations: Go through your codebase and update deprecated parts.
- Test in Staging: Deploy and test in a staging environment before production.
- Update Dependencies: Make sure all libraries and packages are PHP 8.4 compatible.
- Backup Applications: Backup your applications to prevent data loss.
- Use New Features: Refactor code to use new features of PHP 8.4 like property hooks and asymmetric visibility.
PHP 8.4 is awesome with new updation and features, upgrade now for use all new features!



