PHP 8.4: What’s New For Developers

Introduction

Let’s take a closer look at the exciting new features and improvements that have been announced as PHP 8.4 approaches release. Everything you need to know about PHP 8.4 and how it can enhance your development process is covered in this essay.

Release schedule

The official release of PHP 8.4 is slated for November 21, 2024. Before this release, there will be six months of Alphas, Betas, and Release Candidates pre-release stages. This guarantees a reliable and solid release.

New array find functions

The introduction of new array find functions is one of PHP 8.4’s most noteworthy innovations. Among them are:

  • array_find()
  • array_find_key()
  • array_any()
  • array_all()

When using arrays, these functions make typical jobs easier. See our post on the PHP 8.4 Array Find Functions for further information.

PHP property hooks

A recent addition, property hooks are modeled after languages such as Kotlin, C#, and Swift. They offer a more refined method of managing changes and property access. As an example, check this:


    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 simplify and improve the maintainability of your code by removing boilerplate code for property getters and setters. See our article on Property Hooks in PHP 8.4 for further details.

new MyClass()->method() Without Parentheses

A change in PHP syntax introduced in version 8.4 lets you instantiate a class and use its methods without requiring extra parentheses. With this change, PHP now more closely resembles other C-based languages, such as TypeScript, Java, and C#.


    // Before PHP 8.4: Wrapping parentheses are required
    $request = (new Request())->withMethod('GET')->withUri('/hello-world');
    
    
    // PHP 8.4: No wrapping parentheses needed
    $request = new Request()->withMethod('GET')->withUri('/hello-world');                        
    

This improvement streamlines the syntax, increasing readability and lowering the possibility of mistakes. For further information, see our article on Class Instantiation in PHP 8.4 Without Extra Parentheses.

Create a dateTime from a Unix timestamp

Creating a DateTime instance from a Unix timestamp becomes more straightforward in PHP 8.4 with the new createFromTimestamp() method. It 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, developers used the createFromFormat() method, which was less intuitive. This new method simplifies date-time handling in PHP.

New mb_ functions

PHP 8.4 extends multi-byte string support by introducing mb_ variants for several common string functions:

  • array_find()
  • array_find_key()
  • array_any()
  • array_all()

These functions maintain the same arguments as their non-multi-byte counterparts, providing better support for multi-byte strings.

Stay tuned for more updates as we get closer to the official release of PHP 8.4. These new features promise to enhance your development experience, making PHP even more powerful and efficient.

For more in-depth articles and updates, keep an eye on our blog at Techvoot Solutions.