"Excuse"?!? Use readonly whenever you can. You should have a good reason when you do not use it. ... Or an application that started many years ago. ;) Answer from eurosat7 on reddit.com
🌐
Stitcher
stitcher.io β€Ί blog β€Ί readonly-classes-in-php-82
Readonly classes in PHP 8.2 | Stitcher.io
October 25, 2022 - PHP 8.2 adds a new way of declaring classes: you can make them readonly. In practice, it means that all properties of that class will be readonly.
🌐
PHP.Watch
php.watch β€Ί versions β€Ί 8.2 β€Ί readonly-classes
`readonly` Classes - PHP 8.2 β€’ PHP.Watch
A class declared as readonly automatically makes all properties readonly. PHP 8.2 deprecates dynamic properties, but readonly classes hard-fail dynamic properties with an Error exception.
Discussions

When should one use read-only properties / classes?
"Excuse"?!? Use readonly whenever you can. You should have a good reason when you do not use it. ... Or an application that started many years ago. ;) More on reddit.com
🌐 r/PHP
22
12
July 1, 2024
Readonly classes in PHP 8.2
Great! Now I can make my objects immutable without spamming readonly everywhere. More on reddit.com
🌐 r/PHP
24
81
October 26, 2022
PHP 7.4 typed properties are a bit of a waste without readonly declarations

Or am I just being paranoid about library users doing stupid things with public properties?

On the contrary, which is why public properties are only advisable when making basic "struct-like" objects to carry data around without expectations of encapsulation.

I agree readonly and other enhancements (like unions, etc.) would significantly improve the utility of prop types (and types in general). But think about what we got as a step in the right direction, for now.

Remember when typed return types were introduced in PHP 7.0 we didn't have nullable typehints. Huge omission. But we got it eventually. And 7.0 was the first step in getting what we have today.

More on reddit.com
🌐 r/PHP
27
13
August 15, 2018
Readonly properties RFC by Nikita
Have always wanted const properties for a class. Maybe a function parameter can also be readonly , like C++ allows More on reddit.com
🌐 r/PHP
57
117
September 6, 2020
🌐
PHP
php.net β€Ί manual β€Ί en β€Ί language.oop5.properties.php
PHP: Properties - Manual
Example #5 Illegal initialization of readonly properties Β· <?php class Test1 { public readonly string $prop; } $test1 = new Test1; // Illegal initialization outside of private scope.
🌐
PHP
wiki.php.net β€Ί rfc β€Ί readonly_classes
PHP: rfc:readonly_classes
August 4, 2021 - PHP 8.1 added support for readonly properties via PHP RFC: Readonly properties 2.0. However, it's still not easy to declare (quasi-)immutable classes, especially if they contain many properties.
🌐
Beyond Co., Ltd.
beyondjapan.com β€Ί https://beyondjapan.com/en/ β€Ί blog β€Ί web system development β€Ί i tried out readonly class added in php 8.2!
I tried out ReadOnly Class added in PHP 8.2! | Beyond Co., Ltd.
April 10, 2023 - By using the readonly class, you can save time by not having to set the readonly modifier for each property. Once a value is set, it cannot be changed, and dynamic properties can be prohibited, so it seems possible to write more robust code.
🌐
DEV Community
dev.to β€Ί erlandmuchasaj β€Ί final-and-readonly-classes-in-php-38fi
Final and Readonly Classes in PHP - DEV Community
March 21, 2023 - // PHP 8.2+ // Fatal error: Uncaught Error: Cannot modify readonly property ParentClass::$name $obj ->name = 'Ndershkuesi'; This happens because all properties are implicitly readonly when you mark the class as readonly.
🌐
Reddit
reddit.com β€Ί r/php β€Ί when should one use read-only properties / classes?
r/PHP on Reddit: When should one use read-only properties / classes?
July 1, 2024 -

I discovered this feature a few weeks ago and end up using it a lot in my DTOs (most times they end up being read-only classes).

Right now, the only other use case I have for it is for class properties injected via dependency injection, which I believe should probably never be changed to anything else than what it was first instantiated as.

I'm not sure if the DI properties is a good excuse for using read-only, or if there are other use cases I might have missed, which is why I'm asking how you guys use it :)

Find elsewhere
🌐
phpFashion
phpfashion.com β€Ί en β€Ί php-readonly-properties
The Hidden Surprises of PHP Readonly Properties Β» phpFashion
That's exactly what PHP 8.1 delivered with readonly properties. Think of it as giving your objects a safety vault – keeping their data secure from accidental changes. Let's explore how this powerful feature can streamline your code and what gotchas you need to watch out for. ... class User { public readonly string $name; public function setName(string $name): void { $this->name = $name; // First assignment - all OK } } $user = new User; $user->setName('John'); // Great, name is set echo $user->name; // "John" $user->setName('Jane'); // BOOM!
🌐
dailycomputerscience
dailycomputerscience.com β€Ί post β€Ί complete-guide-on-readonly-properties-in-php
Complete Guide on Readonly Properties in PHP
October 11, 2024 - Cleaner, Simpler Code: Instead of manually managing property immutability through getter-only methods, readonly properties simplify the process by allowing you to declare immutability directly in the class definition.
🌐
JetBrains
jetbrains.com β€Ί help β€Ί inspectopedia β€Ί PhpClassCanBeReadonlyInspection.html
Class can be 'readonly' | Inspectopedia Documentation
September 18, 2025 - Reports the classes which contain only readonly properties and can have the readonly modifier added to them. Read-only classes are supported in PHP 8.2 and later.
🌐
DEV Community
dev.to β€Ί neloyahmed β€Ί the-story-of-readonly-classes-and-properties-in-php-1l02
The story of readonly classes and properties in php - DEV Community
December 20, 2022 - You can initialize the readonly property once, and only from the scope where it has been declared. Any other assignment or modification of the property will result in an Error exception. <?php class User { public readonly string $name; } $john = new User; // Illegal initialization outside of private scope.
🌐
PHP.Watch
php.watch β€Ί versions β€Ί 8.1 β€Ί readonly
Readonly Properties - PHP 8.1 β€’ PHP.Watch
The $user object itself can change, and the readonly property does not prevent modifications to the object itself. If the immutability is required, make sure to clone the object prior to assigning: class Post { public readonly User $author; public function __construct(User $author) { - $this->author = $author; + $this->author = clone $author; } }