DEV Community

Cover image for Exploring the Key Updates in PHP 8.4
Asaba William
Asaba William

Posted on

Exploring the Key Updates in PHP 8.4

PHP 8.4 is set to bring a variety of new features and enhancements that aim to improve the developer experience, optimize performance, and simplify coding practices. Here's an overview of the most significant updates:

1. Property Hooks

One of the most anticipated features in PHP 8.4 is the introduction of property hooks. Inspired by languages like Kotlin and C#, property hooks allow developers to define get and set hooks directly on class properties. This feature reduces the need for boilerplate code, such as explicit getters and setters, and provides greater control over property access and modifications.

Example Usage:

class User {
    private bool $isModified = false;

    public string $fullName {
        get => $this->first . " " . $this->last;
        set {
            [$this->first, $this->last] = explode(' ', $value, 2);
            $this->isModified = true;
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

With property hooks, you can customize the behaviour of properties when they are read or modified, making code more expressive and reducing verbosity. You can even define property hooks in interfaces to enforce consistent property behaviour across implementations.

2. Simplified new Syntax

PHP 8.4 eliminates the need for parentheses when chaining methods, properties, or constants directly after instantiating a class. This change simplifies code and enhances readability.

Before PHP 8.4:

$name = (new MyClass())->execute();
Enter fullscreen mode Exit fullscreen mode

With PHP 8.4:

$name = new MyClass()->execute();
Enter fullscreen mode Exit fullscreen mode

This update also applies to constants and static methods, allowing for more fluid and concise code expressions.

3. JIT (Just-In-Time) Enhancements

PHP 8.4 introduces changes to the JIT configuration options, improving the way JIT is enabled or disabled. Previously, you had to set opcache.jit_buffer_size to 0 to disable JIT, but now it can be disabled directly with opcache.jit=disable. These changes aim to streamline configuration and enhance performance by reducing memory usage in certain cases.

4. Deprecation of Implicit Nullable Types

PHP 8.4 deprecates the implicit behaviour of marking a typed variable as nullable if it has a default null value. Now, developers must explicitly declare a nullable type using the ? syntax.

Deprecated Behavior:

function save(Book $book = null) {} // Implicitly nullable
Enter fullscreen mode Exit fullscreen mode

Recommended Approach:

function save(?Book $book = null) {} // Explicitly nullable
Enter fullscreen mode Exit fullscreen mode

This change aims to make code behaviour more explicit and prepare for future versions where implicit nullability will be removed.

5. New HTML5 Support

A new \Dom\HTMLDocument class has been introduced to fully support HTML5 parsing, replacing the older \DOMDocument class, which was not entirely compatible with HTML5. This update simplifies the handling of modern HTML content in PHP applications.

6. New Array Functions

PHP 8.4 adds several new array functions, including array_find(), array_find_key(), array_any(), and array_all(). These functions make it easier to perform common array operations, such as finding elements based on a callback or checking if any elements satisfy a condition.

Example Usage:

$firstMatch = array_find($posts, function (Post $post) {
    return strlen($post->title) > 5;
});
Enter fullscreen mode Exit fullscreen mode

These native functions reduce the need for third-party collection libraries and offer more intuitive operations on arrays.

Conclusion

PHP 8.4 is packed with features that aim to simplify code and enhance the developer experience. With property hooks, simplified new syntax, JIT improvements, and new array functions, PHP 8.4 is a significant step forward in modernizing the language. For more details on these features, visit the official PHP 8.4 RFC page.

By embracing these updates, developers can write cleaner, more efficient code and leverage new capabilities that were previously unavailable in PHP.

Top comments (0)