Readonly Classes in PHP: A Useful Addition to Readonly Properties

Last year around this time, I asked the question whether readonly properties replace getters. I gave a short overview about “best practices” on object oriented programming, summarized the idea of immutable objects and demonstrated how readonly properties helps writing better and more robust code with PHP, before I finalized the blog with my opinion how to combine readonly properties with public getters. Today, I want to talk about a similar concept: readonly classes.

readonly classes in php 8.2
Readonly classes visualize as structures and elements within a digital landscape.

What are Readonly Classes?

As the name suggests, readonly classes are designed to have their values set only once, after which they become read-only. Compared to readonly properties, the syntax is very similar:

readonly class:

Readonly classes have a syntactical advantage over readonly properties: the readonly keyword is only used once instead of for every property. This is useful especially in situations with a lot of dependencies1. But they go beyond that.

Readonly Classes: What is New?

Apart of syntactical sugar, readonly classes provide further advantages and characteristics. The following list is not complete, but rather represents the most important ones in my opinion:

No Inheritance of Non-Readonly Classes

A non-readonly class can not inherit from a readonly class! This makes sense for the nature of readonly classes as otherwise, they would not be readonly anymore. Further, declaring classes readonly may enhance encapsulation for package or framework developers.

Want to know why inheritance is a bad concept bad at all? I adressed the question When to use Traits in a previous article and have set out my thoughts why inheritance leads to a mess and how Traits make it worse.

Typed Properties Only

As with readonly properties, classes declared readonly can only contain typed properties. This is required by PHP but makes apart of that much sense since it makes the code more robust.

No Static Properties

As with “Typed Properties Only”, static properties are also disallowed by PHP. Personally, I think this is a good decision too since “static-ness” makes code less robust and clean.

No Default Values

Classes declared readonly do not allow default values. The value of each property has to be set explictely.

Looking for a PHP developer?

Bring your projects to a next level professionalism with my PHP development services – reach out now for reliable and high-quality solutions.

Dogan Ucar


    Conclusion

    As I said in the conclusion part of my readonly properties blog post, I think introducing readonly-ness to PHP was and is a good decision.

    In conclusion of this post, the introduction of classes as an addition to properties in PHP marks a significant advancement in the language’s capabilities. These features significantly contribute to the robustness and predictability of code, making software development less prone to errors. By aligning with the SOLID principles, they ensure that code is more maintainable and adaptable to change, which is crucial in today’s fast-paced software development landscape.

    My experience with using these features in Keestash, the open source password manager, has been overwhelmingly positive. The readonly attributes have enhanced the stability and reliability of the application, ensuring a smoother and more secure user experience. I’ve seen firsthand how they contribute to creating a more solid, error-resistant foundation for software, which is invaluable in a professional setting.

    If you’re interested in seeing the practical benefits of readonly properties and classes in action, I encourage you to explore Keestash. It’s not just a proof to the power of these PHP features, but also an excellent resource for securing your passwords efficiently and effectively. Click here to register and experience the difference in storing passwords securely or here to learn more about Keestash.

    Footnotes

    1. Even you should avoid having too much dependencies at all.