What's New in PHP 8.4: Exciting Features & Performance Improvements

What's New in PHP 8.4: Exciting Features & Performance Improvements

PHP continues to evolve at a rapid pace. Following the successes of PHP 8.1, 8.2, and 8.3, the release of PHP 8.4 introduces several features that make code cleaner, safer, and much more modern.

In this article, we’ll dive into the most notable updates and features introduced in PHP 8.4.

1. Property Hooks

Perhaps the biggest change in PHP 8.4 is the introduction of property hooks. If you have ever used C# or Kotlin, this concept will look familiar. Property hooks allow you to define custom logic for reading and writing properties directly inside the class definition, eliminating the boilerplate code of simple getters and setters.

class User {
    public string $name {
        // Set hook
        set {
            if (empty($value)) {
                throw new InvalidArgumentException("Name cannot be empty");
            }
            $this->name = trim($value);
        }
        // Get hook
        get => ucfirst($this->name);
    }
}

2. New Array Helper Functions

Working with arrays in PHP is getting cleaner. PHP 8.4 introduces helper functions that make searching arrays more functional and direct.

  • array_find(): Returns the first element matching a callback.
  • array_find_key(): Returns the key of the first element matching a callback.
  • array_any(): Returns true if at least one element matches the callback.
  • array_all(): Returns true if all elements match the callback.
$users = [
    ['name' => 'Alice', 'role' => 'admin'],
    ['name' => 'Bob', 'role' => 'editor'],
];

$admin = array_find($users, fn($u) => $u['role'] === 'admin');

3. Asymmetric Visibility

You can now define different visibility rules for reading and writing a property. This is particularly useful for building Immutable Data Transfer Objects (DTOs) or domain models.

class Product {
    // Read: public, Write: private
    public private(set) string $sku;

    public function __construct(string $sku) {
        $this->sku = sku;
    }
}

4. Direct Method Chaining on New Instances

In previous versions of PHP, if you wanted to chain a method call immediately after creating a new object, you had to wrap it in parentheses (e.g., (new Controller())->index()). PHP 8.4 removes this restriction.

// Now valid syntax!
$response = new MyService()->fetchData();

PHP 8.4 represents another massive step forward for modern backend development, encouraging developers to write less boilerplate and leverage cleaner syntax.

Share:

Get Started on Creating Your Web Apps
with a Reliable Service Today!

Expert WordPress, Shopify & WooCommerce development — on time, every time.

Get a Free Quote →