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?
You must log in or register to comment.