The PHP development team has officially released PHP 8.4, introducing a suite of powerful new features that promise to enhance productivity and make the language more intuitive for developers.

Key highlights include property hooks, new array find functions, class instantiation without extra parentheses, and much more. Here’s a detailed look at the major updates in PHP 8.4. 

New Array Find Functions 

PHP 8.4 brings four new array helper functions that simplify checking arrays for specific conditions: 

  • array_find() 
  • array_find_key() 
  • array_any() 
  • array_all() 

The array_find() Function 

The array_find($array, $callback) function returns the first element in an array for which the callback evaluates to true. 

The array_find() Function 

The array_find_key() Function 

The array_find_key($array, $callback) function works similarly but returns the key of the first matching element. 

The array_find_key() Function 

The array_any() and array_all() Functions 

  • array_any() checks if any array elements satisfy the condition. 
  • array_all() verifies if all elements meet the condition. 
The array_any() and array_all() Functions 

Property Hooks 

Inspired by languages like Kotlin and C#, property hooks provide get and set hooks for object properties. This feature simplifies property access logic while eliminating boilerplate code. 

Example Usage 

Property Hooks 

With property hooks, you can directly manage custom logic for reading and writing properties without relying on generic methods like __get and __set. 

Class Instantiation Without Extra Parentheses 

PHP 8.4 simplifies object instantiation by removing the requirement for surrounding parentheses when chaining methods or accessing properties. 

Before PHP 8.4 

Before PHP 8.4 

In PHP 8.4 

This quality-of-life improvement aligns PHP with other C-family languages, making code cleaner and easier to read. 

This quality-of-life improvement aligns PHP with other C-family languages, making code cleaner and easier to read. 

Create a DateTime from a Unix Timestamp 

PHP 8.4 introduces the createFromTimestamp() method in the DateTimeImmutable class for creating instances directly from Unix timestamps. 

Create a DateTime from a Unix Timestamp 

The method also supports timestamps with microseconds, streamlining the creation of precise date-time objects. 

New Multibyte String Functions 

PHP 8.4 expands support for multibyte strings with the following functions: 

  • mb_trim() 
  • mb_ltrim() 
  • mb_rtrim() 
  • mb_ucfirst() 
  • mb_lcfirst() 

These functions enable trimming and case transformations on multibyte strings, enhancing support for non-ASCII text. 

Asymmetric Property Visibility 

Asymmetric property visibility allows different levels of access for reading and writing properties. 

Asymmetric Property Visibility 

This feature offers fine-grained control over property access, enabling public reads while restricting writes. 

Conclusion 

The release of PHP 8.4 brings a significant set of enhancements to the language. These new features offer improved developer experience, promote cleaner code, and streamline various operations.

With these advancements, PHP remains a powerful and versatile language for web development. 

Further Resources