PHP 8.4 Most useful features

Written on 9th Feb 2025
It will take between 2 and 3 minutes to read this article

PHP 8.4: The Most Useful New Features

PHP 8.4, released on 21 November 2024, introduces several noteworthy features and improvements that enhance the language's efficiency and developer experience. Here's an overview of the most impactful additions:

Property Hooks

Property hooks allow developers to define custom behaviour when accessing or modifying class properties, reducing the need for boilerplate getter and setter methods. This feature streamlines code and enhances readability.

Example:

class User
{
private string $firstName;
private string $lastName;
 
public string $fullName
{
get => "{$this->firstName} {$this->lastName}";
set
{
[$this->firstName, $this->lastName] = explode(' ', $value, 2);
}
}
}
 
$user = new User();
$user->fullName = 'Jane Doe';
echo $user->fullName; // Outputs: Jane Doe

In this example, the fullName property dynamically constructs or deconstructs the user's full name, eliminating the need for separate methods.

Asymmetric Visibility

This feature allows different visibility levels for reading and writing class properties. For instance, a property can be publicly readable but only modifiable within the class, enhancing encapsulation.

Example:

class Product
{
public private(set) float $price;
 
public function __construct(float $price)
{
$this->price = $price;
}
 
public function applyDiscount(float $discount): void
{
$this->price -= $discount;
}
}
 
$product = new Product(100.0);
echo $product->price; // Outputs: 100
$product->applyDiscount(10.0);
echo $product->price; // Outputs: 90
$product->price = 80.0; // Error: Cannot modify property

Here, the price property is publicly accessible for reading but can only be modified within the class methods.

Chaining new Without Parentheses

PHP 8.4 allows method chaining directly upon object instantiation without requiring additional parentheses, simplifying syntax.

Example:

$request = new Request()->setMethod('GET')->setUri('/home');

Previously, this would have required parentheses around the new expression.

New Array Functions

PHP 8.4 introduces several array functions to enhance data handling:

  • array_find(): Returns the first element that satisfies a given condition.
  • array_find_key(): Returns the key of the first element that meets a specified condition.
  • array_any(): Checks if any array element satisfies a condition.
  • array_all(): Verifies if all elements meet a particular condition.

Example:

$numbers = [1, 2, 3, 4, 5];
 
$firstEven = array_find($numbers, fn($n) => $n % 2 === 0);
echo $firstEven; // Outputs: 2
 
$hasNegative = array_any($numbers, fn($n) => $n < 0);
var_dump($hasNegative); // Outputs: bool(false)

These functions simplify common array operations, reducing the need for manual iteration.

HTML5 Support in DOM

The new Dom\HTMLDocument class provides standards-compliant support for parsing HTML5 documents, addressing previous limitations in handling modern HTML structures.

Example:

use Dom\HTMLDocument;
 
$html = '<main><article>PHP 8.4 is here!</article></main>';
$doc = HTMLDocument::createFromString($html);
$article = $doc->querySelector('main > article');
echo $article->textContent; // Outputs: PHP 8.4 is here!

This enhancement ensures better compatibility with contemporary web standards.

Multibyte String Functions

PHP 8.4 adds multibyte support for trimming and case conversion functions:

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

Example:

$str = " こんにちは ";
echo mb_trim($str); // Outputs: こんにちは
 
$str = "こんにちは";
echo mb_ucfirst($str); // Outputs: こんにちは

These functions enhance handling of multibyte character encodings, crucial for internationalisation.


In summary, PHP 8.4 introduces significant features that streamline code, enhance performance, and improve compatibility with modern standards, making it a compelling upgrade for developers.

Code highlighting provided by torchlight.dev.