A Look at What's Coming to PHP 8.4


PHP 8.4 is on the horizon, bringing with it a host of exciting new features and improvements. Scheduled for release on November 21, 2024, PHP 8.4 promises to enhance the developer experience with several key updates. This post will delve into what has been announced so far and what new features you can expect.


When is PHP 8.4 Scheduled to Be Released?


PHP 8.4 is set to be released on November 21, 2024. Before the official release, it will undergo a six-month pre-release phase, including Alpha, Beta, and Release Candidate stages. This phased approach ensures that the new features and improvements are thoroughly tested and refined before the final release.


New Array Find Functions


One of the most anticipated features in PHP 8.4 is the introduction of new array find functions. These functions are designed to simplify common tasks when working with arrays:


array_find(): Finds the first element in an array that satisfies a given condition.

array_find_key(): Finds the key of the first element in an array that satisfies a given condition.

array_any(): Checks if any element in an array satisfies a given condition.

array_all(): Checks if all elements in an array satisfy a given condition.


These new functions will make array operations more intuitive and less verbose. For a more detailed look, check out our post on the PHP 8.4 Array Find Functions.


PHP Property Hooks


Property hooks in PHP 8.4 are inspired by languages like Kotlin, C#, and Swift. They provide a way to define custom logic for property access and modification directly within the class definition. Here’s a basic example:


class User implements Named

{

    private bool $isModified = false;


    public function __construct(

        private string $first,

        private string $last

    ) {}


    public string $fullName {

        // Override the "read" action with arbitrary logic.

        get => $this->first . " " . $this->last;


        // Override the "write" action with arbitrary logic.

        set {

            [$this->first, $this->last] = explode(' ', $value, 2);

            $this->isModified = true;

        }

    }

}


Property hooks will help eliminate boilerplate code for property getters and setters, allowing for more concise and maintainable code. For more details, see our post on Property Hooks in PHP 8.4.


New `MyClass()->method()` Without Parentheses


A significant improvement in PHP 8.4 is the ability to access class members without wrapping the instantiation in parentheses. Previously, to avoid a parse error, you had to write:


// Wrapping parentheses are required to access class members

$request = (new Request())->withMethod('GET')->withUri('/hello-world');


With PHP 8.4, this becomes simpler:


// PHP Parse error (<= PHP 8.3): syntax error, unexpected token "->"

$request = new Request()->withMethod('GET')->withUri('/hello-world');


This change eliminates the need for extra parentheses, aligning PHP more closely with languages like Java, C#, and TypeScript. For more information, check out our post on Class Instantiation Without Extra Parentheses in PHP 8.4.


Create a DateTime from a Unix Timestamp


Creating a `DateTime` from a Unix timestamp becomes more straightforward in PHP 8.4 with the new `createFromTimestamp()` method. This method supports both standard Unix timestamps and timestamps with microseconds:


$dt = DateTimeImmutable::createFromTimestamp(1718337072);

$dt->format('Y-m-d'); // 2024-06-14


$dt = DateTimeImmutable::createFromTimestamp(1718337072.432);

$dt->format('Y-m-d h:i:s.u'); // 2024-06-14 03:51:12.432000


Previously, creating a `DateTime` from a timestamp required using the `createFromFormat()` method, which was less intuitive. The new method simplifies this process significantly. For more details, check out our post on Creating DateTime from a Unix Timestamp in PHP 8.4.


Conclusion


PHP 8.4 is shaping up to be a significant update with numerous features designed to enhance the developer experience. From new array find functions to property hooks, simplified class instantiation, and improved DateTime handling, there’s a lot to look forward to. Mark your calendars for November 21, 2024, and get ready to explore the new capabilities of PHP 8.4!