PHP 8.4 is due for release in November 2024

A very quick summary of the most notable changes in PHP 8.4

Image for blog post PHP 8.4 is due for release in November 2024

With PHP 8.4 just around the corner(ish) we're going to run down the changes that we're most interested in.

Property Hooks

Perhaps the biggest change to land in PHP 8.4 is property hooks, these will allow you to drop the boilerplate code you've been using for mutating properties when getting or setting.

Using the example from the RFC, this code:


class User 
{
    private $name;
 
    public function __construct(string $name) {
        $this->name = $name;
    }
 
    public function getName(): string {
        return $this->name;
    }
 
    public function setName(string $name): void {
        $this->name = $name;
    }
}

Could become:


class User 
{
    public string $name {
        set {
            if (strlen($value) === 0) {
                throw new ValueError("Name must be non-empty");
            }
            $this->name = $value;
        }
    }
 
    public function __construct(string $name) {
        $this->name = $name;
    }
}

This setter hook allows some validation on the property without having to use the traditional getName, setName pattern. Whilst this may not look significant it could change the way many objects are managed and provide a standardised way to verify data across your code.

I'm not 100% on the syntax here but I really like the idea.

This pattern opens up the opportunity to do things like this:


class User
{
    public function __construct(private string $first, private string $last) {}
 
    public string $fullName {
        get { 
            return $this->first . " " . $this->last;
        }
    }
}
 
$u = new User('Lisa', 'Simpson');
 
// prints "Lisa Simpson"
print $u->fullName;

What's not to like?

new MyClass()->method() without parentheses

This one is great, it's not a significant problem with the existing syntax but this is just such a cleaner way to write this kind of code.

Consider the current state of this syntax in PHP8.3:


// OK
$request = (new Request())->withMethod('GET')->withUri('/hello-world');
 
// PHP Parse error: syntax error, unexpected token "->"
$request = new Request()->withMethod('GET')->withUri('/hello-world');

Essentially this RFC is going to allow the second syntax to work, and that's a good thing, especially for those of us who struggle to follow code that starts to look like this ))))->something() .

Implicitly nullable parameter declarations deprecated

This is essentially just a deprecation on declaring a null parameter value without having that null type explicity type hinted.

So effectively this

function doSomething(string $name = null)

Will fire a deprecation warning, it'll need fixing like this

function doSomething(?string $name = null)

Or this

function doSomething(string|null $name = null)

This one is going to generate a lot of deprecation warnings out there in the wild. It's probably not that hard to find these and update them, but in some projects that's probably a fair bit of work. Seems like the right thing to do though, it's weird to allow this in hindsight, hadn't really considered it before.

Regular expression changes

PHP's preg_ functions rely on the Perl Regex library (Perl-Compatible Regular Expressions, PCRE). In PHP 8.4 the latest version of this library is compiled in to PHP, so there are a few changes to how the underlying engine works.

From what I can tell the changes shouldn't really affect the bulk of regex in user-land, it seems that the changes mostly allow syntax that currently isn't valid to be valid, so I'd expect any use of this in previous versions would have resulted in errors or incorrect results anyway.

So, hopefully this won't require any changes, if everybody has been good and written their unit tests around any complex regex then there should be no need to worry anyway!

Other changes of note:

  • New request_parse_body function
  • New DateTime and DateTimeImmutable createFromTimestamp method
  • New Multibyte string functions: mb_trim, mb_ltrim, and mb_rtrim



Photo by Shahadat Rahman on Unsplash


Article Category Tags

Code PHP Web Development