Wednesday, October 30, 2024

PHP 8.4: New features & release date

Rutger Broerze
PHP 8.4: New features & release date image

When will PHP 8.4 be released?

PHP 8.4 was released on November 21, 2024. It contains some exiting new features.


What new features can we expect in PHP 8.4?

  • Property hooks
  • Asymmetric visibility
  • New array functions
  • Parsing HTML 5
  • Class instantiation without parentheses

Property hooks

PHP 8.4 introduces property hooks, marking one of the biggest shifts in how properties are managed in PHP. This feature lets developers attach “get” and “set” hooks directly to properties, reducing boilerplate code.

Key benefits

  • Less boilerplate: Property hooks reduce the need for manual getters and setters.
  • Interface support: Property hooks can be defined in interfaces, allowing standardized class behavior.

Example usage

class Product
{
    public function __construct(private float $basePrice) {}

    public string $price {
        get => '$' . number_format($this->basePrice, 2);

        set (float $newPrice) {
            if ($newPrice < 0) throw new InvalidArgumentException("Price cannot be negative.");
            $this->basePrice = $newPrice;
        }
    }
}

Asymmetric visibility

In PHP, managing property access has always posed challenges, especially when balancing readability and immutability. PHP 8.4’s asymmetric visibility solves this by allowing different access levels for reading and modifying properties.

Why use asymmetric visibility?

  • Allows public read access while restricting modifications to internal code.
  • Ideal for data structures that need validation but not full immutability.

Example usage

class Example
{
    public private(set) string $status = 'active';
}

New array functions

PHP 8.4 introduces four new array functions for simpler searching and validation.

Here’s a quick look at what each function does:

  • array_find: Returns the first value that satisfies the callback.
  • array_find_key: Returns the key of the first item that matches the callback.
  • array_any: Returns true if at least one item in the array passes the callback check.
  • array_all: Returns true only if all items in the array pass the callback check.

Parsing HTML 5

With PHP 8.4, a new \Dom\HTMLDocument class enables easier HTML5 parsing, solving issues that existed with \DOMDocument.

Example usage

$htmlContent = '<!DOCTYPE html><html><body><p>Hello, World!</p></body></html>';
$doc = \Dom\HTMLDocument::createFromString($htmlContent);

Chaining methods on class instantiation without parentheses

PHP 8.4 improves method chaining by removing the need for parentheses around class instantiation, making code more readable.

Before PHP 8.4

$user = (new User())->setName('Alice')->setEmail('[email protected]');

With PHP 8.4

Now, you can write it without parentheses:

$user = new User()->setName('Alice')->setEmail('[email protected]');

Will Laravel support PHP 8.4?

Yes, it will! Laravel 11 supports PHP 8.2 and higher, which means PHP 8.3 and PHP 8.4 are supported as well.

It takes only one minute to get started

Start monitoring your Laravel applications today

Websites will go down, and errors will happen. With Sorane, you are sure to catch any errors and performance issues the moment they happen.

Takes 1 minute to get started.
No credit card required. Cancel anytime.