PHP 8.4, the latest iteration of the popular scripting language, brings a host of new features, performance optimizations, and developer-friendly enhancements. Released in November 2024, this version continues PHP’s tradition of evolving to meet modern development needs while maintaining its ease of use. Let’s dive into some of the standout features and updates in PHP 8.4.

1. Property Hooks

One of the most exciting additions is property hooks, which allow developers to define custom behaviors when accessing or modifying properties in a class. This feature provides better control over object property management, improving encapsulation and flexibility.

class MyClass {
    private $data = [];

    public function __get($name) {
        echo "Accessing property '$name'\n";
        return $this->data[$name] ?? null;
    }

    public function __set($name, $value) {
        echo "Setting property '$name' to '$value'\n";
        $this->data[$name] = $value;
    }
}

$obj = new MyClass();
$obj->name = "PHP 8.4"; // Setting property 'name' to 'PHP 8.4'
echo $obj->name;        // Accessing property 'name'

2. Asymmetric Property Visibility

PHP 8.4 introduces asymmetric property visibility, allowing different access levels for getting and setting properties. This feature offers fine-grained control over property access and modification.

class Example {
    public function __construct(
        public readonly int $readOnly = 42, // Public getter, private setter
        private int $privateWrite = 10,    // Private getter and setter
    ) {}
}

$obj = new Example();
echo $obj->readOnly; // Works (public getter)
// $obj->readOnly = 50; // Error: Cannot set a readonly property

3. Enhanced Array Functions

PHP 8.4 introduces several new array utility functions to simplify common operations:

array_find and array_find_key

These functions locate the first matching element or key in an array.

$array = [10, 20, 30, 40];
$result = array_find($array, fn($value) => $value > 25);
echo $result; // 30

$key = array_find_key($array, fn($value) => $value === 20);
echo $key; // 1

array_any and array_all

Check if any or all elements in an array meet a condition.

$array = [1, 2, 3, 4];
echo array_any($array, fn($x) => $x > 3); // true
echo array_all($array, fn($x) => $x > 0); // true

4. Multibyte String Support Enhancements

Handling multibyte strings is now easier with functions like mb_trim, mb_ucfirst, and mb_lcfirst. These functions simplify text processing in multilingual applications.

$str = "   ñandú   "; // Multibyte string
echo mb_trim($str); // "ñandú"
echo mb_ucfirst($str, "UTF-8"); // Ñandú

5. Lazy Objects

Lazy objects defer instantiation until the object is actually accessed. This can significantly improve performance, especially when working with resource-intensive operations.

class HeavyClass {
    public function __construct() {
        echo "Object created\n";
    }
}

$lazyObject = new LazyObject(fn() => new HeavyClass());
// The object is created only when accessed

6. Improved JIT Compilation

PHP 8.4 introduces a new JIT implementation based on the IR Framework, further boosting performance in computation-heavy scenarios. This improvement makes PHP more competitive in tasks traditionally dominated by other languages.

function heavyComputation() {
    $sum = 0;
    for ($i = 0; $i < 1_000_000; $i++) {
        $sum += sqrt($i);
    }
    return $sum;
}

echo heavyComputation();

7. BCMath Object API

PHP 8.4 introduces an object-oriented API for BCMath, simplifying arithmetic operations on large numbers.

$num1 = new BCMath\Number("123456789123456789");
$num2 = new BCMath\Number("987654321987654321");

$result = $num1->add($num2);
echo $result->toString(); // 1111111111111111110

8. Security Enhancements: Default Bcrypt Cost

The default cost for bcrypt has been increased from 10 to 12, enhancing password security by making it computationally more expensive to brute-force passwords.

$options = ['cost' => 12];
echo password_hash('mypassword', PASSWORD_BCRYPT, $options);

Final Thoughts

PHP 8.4 continues to push the boundaries of what the language can achieve, delivering features that simplify development while improving performance and security. Whether you’re managing complex applications or building multilingual, high-performance systems, PHP 8.4 has something for everyone. Upgrade today to take advantage of these cutting-edge features!