Readonly properties
Readonly properties are an awesome new feature for modern PHP code that is embracing patterns such as immutable data objects. Immutable objects are inherently extremely safe and predictable as they simply cannot change. This makes for reliable and easy-to-comprehend code.
The feature is deceptively simple. There is a new readonly
keyword that you can prefix a class property with (including in constructor promotion) and that denotes that property as, well, read-only.
What this really means is that it is write-once, and read-only from that point on. The value can be set in the constructor or anywhere else within the class methods. It cannot be set from outside the class. Once that value has been set, it's locked.
The classic way to create an immutable DTO would be something like this:
src/Part4/Chapter12/ReadOnly/ClassicImmutable.php
Repo: https://git.io/JRw5a
<?php declare(strict_types=1); namespace Book\Part4\Chapter12\ReadOnly; class C...