Home
New in PHP 8.5: Final Promoted Properties
Liked 6 times

PHP 8.5 adds support for final properties using constructor promotion. In this (very short) article, I’ll show you everything you need to know about this new addition.

Starting with PHP 8.5, you'll be able to do the following:

 public function __construct(
    final public string $someProperty,
) {}

This wasn't possible before, as promoted properties couldn't be declared final.

Perhaps the more interesting part is that you can now omit the visibility modifier if you include final. In that case, the property will default to public:

 public function __construct(
    final string $someProperty, // this property will be public
) {}

Personally, I’m not a fan of this behavior — I prefer explicit over implicit. Fortunately, it can be enforced by third-party tools like code style fixers. Still, I would have preferred if the core required the visibility to be specified.

What do you think? Do you like this change, or would you have preferred a stricter approach?


More from New in PHP 8.5:

© 2024 Dominik Chrástecký. All rights reserved.