DEV Community

Cover image for PHP 8.4: What to Expect, What’s New, and Why It’s a Big Deal for Developers
HichemTech
HichemTech

Posted on • Edited on

PHP 8.4: What to Expect, What’s New, and Why It’s a Big Deal for Developers

Surprise! PHP 8.4 was supposed to land on November 21, 2024, but the PHP team decided to treat us early. PHP 8.4.0 officially dropped on November 19, and, as if that wasn’t enough, PHP 8.4.1 followed the next day on November 20. Talk about efficiency! Let’s dive into what’s new, why it’s great, and why developers everywhere should be excited about this release.


What’s New and Why It Matters

1. Property Hooks: Your New Favorite Feature

Property hooks allow you to define custom behavior for reading, writing, and unsetting properties. Before PHP 8.4, you had to rely on magic methods (__get, __set) for this functionality, which were often hard to debug and lacked IDE support. Now, you can define hooks directly on properties.

Before PHP 8.4

class Example {
    private array $data = [];

    public function __get(string $name) {
        return $this->data[$name] ?? null;
    }

    public function __set(string $name, $value) {
        $this->data[$name] = $value;
    }
}

$example = new Example();
$example->name = "PHP";
echo $example->name; // Outputs: PHP
Enter fullscreen mode Exit fullscreen mode

With PHP 8.4

class Example {
    public string $name = "Mohamed" {
        get => "Property: $this->name";
        set => throw new Exception("This property is read-only.");
    }
}

$example = new Example();
echo $example->name; // Outputs: Property: PHP
$example->name = "New value"; // Throws exception
Enter fullscreen mode Exit fullscreen mode

Using an Interface with Property Hooks

PHP 8.4 also allows property hooks to be declared in interfaces, making it easier to define consistent property behavior across classes:

interface HasAuthors {
    public string $credits { get; }
    public Author $mainAuthor { get; set; }
}

class Book implements HasAuthors {
    public string $credits {
        get => "Authors: Mohamed, Fatima";
    }

    public Author $mainAuthor {
        get => new Author("Mohamed");
        set => throw new Exception("Main author cannot be changed.");
    }
}

$book = new Book();
echo $book->credits; // Outputs: Authors: Mohamed, Fatima
Enter fullscreen mode Exit fullscreen mode

2. Method Chaining Without Parentheses

Before PHP 8.4, chaining methods on a new object required extra parentheses, which could clutter the code.

Before PHP 8.4

$result = (new MyClass())->firstMethod()->secondMethod();
Enter fullscreen mode Exit fullscreen mode

With PHP 8.4

$result = new MyClass()->firstMethod()->secondMethod();
Enter fullscreen mode Exit fullscreen mode

The difference is subtle but significant, especially in complex chaining scenarios.


3. Asymmetric Visibility

PHP 8.4 introduces the ability to set different visibility levels for property getters and setters, improving encapsulation.

Before PHP 8.4

class User {
    private string $name;

    public function getName(): string {
        return $this->name;
    }

    public function setName(string $name): void {
        $this->name = $name;
    }
}
Enter fullscreen mode Exit fullscreen mode

With PHP 8.4

class User {
    public string $name {
        get;
        private set;
    }
}

$user = new User();
echo $user->name; // Works
$user->name = "New Name"; // Throws error: private setter
Enter fullscreen mode Exit fullscreen mode

This makes your code cleaner and ensures stricter access control.


4. array_find()

Finding specific values in arrays often required writing custom loops or array functions. PHP 8.4 introduces array_find() for easier searching.

Before PHP 8.4

$numbers = [1, 2, 3, 4];
$found = null;

foreach ($numbers as $number) {
    if ($number > 2) {
        $found = $number;
        break;
    }
}

echo $found; // Outputs: 3
Enter fullscreen mode Exit fullscreen mode

With PHP 8.4

$numbers = [1, 2, 3, 4];
$result = array_find($numbers, fn($value) => $value > 2);

echo $result; // Outputs: 3
Enter fullscreen mode Exit fullscreen mode

5. Deprecation of Implicit Nullable Types

Implicit nullable parameters are deprecated in PHP 8.4, improving clarity. You must now explicitly declare nullable parameters.

Before PHP 8.4

function greet($name = null) {
    echo $name ?? "Hello, World!";
}
Enter fullscreen mode Exit fullscreen mode

With PHP 8.4

function greet(?string $name = null) {
    echo $name ?? "Hello, World!";
}
Enter fullscreen mode Exit fullscreen mode

This ensures better code readability and avoids ambiguous type declarations.


6. New HTML5 Support

PHP’s DOM extension now fully supports HTML5 parsing with the new \DOM\HTMLDocument class.

Before PHP 8.4

Handling HTML5 documents often required third-party libraries or manual string manipulation.

With PHP 8.4

$html = "<!DOCTYPE html><html><body><h1>Hello HTML5!</h1></body></html>";
$doc = new \DOM\HTMLDocument();
$doc->loadHTML($html);
echo $doc->saveHTML();
Enter fullscreen mode Exit fullscreen mode

7. JIT Changes

The Just-In-Time (JIT) compiler has received a significant update in PHP 8.4. Previously, disabling or enabling JIT required setting opcache.jit_buffer_size to 0 or a specific value, which could be unintuitive. Now, there’s a more straightforward way to configure JIT.

Before PHP 8.4

; To disable JIT
opcache.jit_buffer_size=0

; To enable JIT
opcache.jit_buffer_size=64m
Enter fullscreen mode Exit fullscreen mode

With PHP 8.4

; Disable JIT
opcache.jit=disable
opcache.jit_buffer_size=64m

; Enable JIT
opcache.jit=tracing
opcache.jit_buffer_size=64m
Enter fullscreen mode Exit fullscreen mode

If you previously only specified opcache.jit_buffer_size but left out opcache.jit, you’ll now need to explicitly enable JIT using opcache.jit=tracing.

Additionally, PHP 8.4 includes optimizations that make JIT run faster and use less memory in certain cases, benefiting performance-heavy applications like simulations, numerical computations, or real-time analytics.


8. Lazy Objects

PHP 8.4 introduces native support for lazy objects, a feature that is frequently used in frameworks to create proxy objects. Lazy objects are objects whose initialization is delayed until they are actually accessed. This can save memory and improve performance in scenarios where not all properties or methods of an object are used.

Example: Creating a Lazy Object

$initializer = static function (MyClass $proxy): MyClass {
    // Custom initialization logic
    return new MyClass(123);
};

$reflector = new ReflectionClass(MyClass::class);

// Create a lazy proxy object
$object = $reflector->newLazyProxy($initializer);

// The object is initialized only when accessed
echo $object->someMethod();
Enter fullscreen mode Exit fullscreen mode

9. exit and die as Functions

exit and die can now officially be used as functions, with or without parentheses.

Before PHP 8.4

exit "Goodbye!"; // Works
exit("Goodbye!"); // Works, but inconsistent
Enter fullscreen mode Exit fullscreen mode

With PHP 8.4

exit "Goodbye!"; // Still works
exit("Goodbye!"); // Consistent with function syntax
Enter fullscreen mode Exit fullscreen mode

10. Object API for BCMath

Perform arbitrary-precision math with an object-oriented API.


11. The #[Deprecated] Attribute

Mark deprecated functions or classes with a new attribute:

#[Deprecated("Use newMethod() instead.")]
function oldMethod() {
    // ...
}
Enter fullscreen mode Exit fullscreen mode

12. Smaller Additions and Backward Compatibility

  • Improved array_is_list() detection.
  • New multibyte string functions like mb_str_pad().
  • Functions fsync() and fdatasync() for better file integrity.


Why PHP 8.4 Deserves the Hype

This release isn’t just about new features — it’s about making PHP more modern, efficient, and developer-friendly. Whether you’re a framework fanatic like Laravel (I’m a fan of Laravel 🤫), a WordPress wizard, or just tinkering with APIs, there’s something in PHP 8.4 for you.


Why the Early Release?

Who knows? Maybe the PHP team was just as excited as we are. Or maybe they wanted to give us a couple of extra days to play with the new features before the weekend 😁. Either way, it’s here, and it’s awesome.


What’s Next?

Make sure your codebase is ready for PHP 8.4. Check the release notes, test your apps, and enjoy the ride. Oh, and don’t forget to treat yourself to a coffee for upgrading early — you’ve earned it , oh well.. WE’ve earned it 😂.

PHP 8.4 is a game-changer. Dive in and discover what makes this version worth celebrating!

Top comments (3)

Collapse
 
xwero profile image
david duymelinck

Am I missing something? This is not on the PHP website.

Collapse
 
__7d22403a profile image
Constantine Beloglazov

When release? i don't see on the php website