Piotr Jura·
May 17, 2024

What's new in PHP 8.4?

What will change in PHP 8.4?

PHP 8.4 introduces many changes but let's be honest, most are small and/or irrelevant for 99% of us. Here are the changes that really matter though, and are easy to miss when checking out the list.

List of most important PHP 8.4 Changes

Here are two most important changes you should know about. Expected release date is Nov 21, 2024.

Property Hooks - forget about getters/setters

If you've ever written getter or setter methods for private/protected fields of your class (you did that a lot if you've worked with Symfony Doctrine entities), you'll appreciate the change.

Instead of this:

Getter/Setter methods
class Foo
    private int $runs = 0;
    public function getRuns(): int { return $this->runs; }
    public function setRuns(int $runs): void
      if ($runs <= 0) throw new Exception();
      $this->runs = $runs;
$f = new Foo();
$f->setRuns($f->getRuns() + 1);

Can now be written as:

class Foo
    public int $runs = 0 {
        set {
            if ($value <= 0) throw new Exception();
            $this->runs = $value;
$f = new Foo();

Adding getter (get) or setter (set) is optional! The idea is that properties can be safely made public, as using getters/setters, you can control how they are read and written to.

new Class()->method()

You (most probably) won't have to wrap the newly instantiated class, of which method you want to immediately call with parenthesis. Previously, you had to write:

(new Class())->method();

Not wrapping new Class() with parenthesis caused a syntax error.

Now, this (most probably, if the RFC gets into PHP 8.4) will no longer be a syntax error:

new Class()->method();