A readonly class, available since PHP 8.2, makes declared instance properties readonly after initialization.
The examples below begin with the core behavior, then move to an application-oriented use.
How PHP 8.2 Readonly Classes: Value Object Patterns Works
A readonly class, available since PHP 8.2, makes declared instance properties readonly after initialization.
Compatibility note: Requires PHP 8.2 or later.
Example 1: Basic Usage
This example uses a small input to make the main behavior visible.
<?php
readonly class Coordinate { public function __construct(public float $latitude, public float $longitude) {} }
echo (new Coordinate(48.85, 2.35))->latitude;
Output:
48.85
A readonly class, available since PHP 8.2, makes declared instance properties readonly after initialization.
Example 2: A Different Input
A different set of values shows the same operation in another situation.
<?php
final readonly class CurrencyAmount { public function __construct(public int $cents, public string $currency) {} }
$amount = new CurrencyAmount(1999, "EUR");
echo $amount->currency;
Output:
EUR
Readonly is shallow: referenced objects may remain mutable, so it is not a guarantee of deep immutability.
Practical Example: Applying the Feature
This example shows an application-style use of the feature, with its surrounding setup visible.
<?php
final readonly class UserIdentifier { public function __construct(public string $value) { if ($value === "") { throw new InvalidArgumentException("Empty identifier"); } } }
echo (new UserIdentifier("usr-17"))->value;
Output:
usr-17
The code is shown for learning. Adjust the class names, inputs and error handling to suit your application.
Important Behavior and Edge Cases
- Readonly is shallow: referenced objects may remain mutable, so it is not a guarantee of deep immutability.
- A readonly class, available since PHP 8.2, makes declared instance properties readonly after initialization.
- Observe differences in empty inputs, invalid states and optional extension availability where relevant.
Comparison With Related Approaches
| Approach | Important Difference |
|---|---|
PHP 8.2 Readonly Classes: Value Object Patterns | A readonly class, available since PHP 8.2, makes declared instance properties readonly after initialization. |
| Alternative approach | A normal class requires individual readonly declarations when only some properties should be protected. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
Ignoring the documented state or precondition of PHP 8.2 Readonly Classes: Value Object Patterns | Readonly is shallow: referenced objects may remain mutable, so it is not a guarantee of deep immutability. |
| Expecting a different type of result | Read the declared return type and distinguish returned values from changes to the source object. |
| Assuming a related API works identically | A normal class requires individual readonly declarations when only some properties should be protected. |
Key Takeaways
- A readonly class, available since PHP 8.2, makes declared instance properties readonly after initialization.
- Readonly is shallow: referenced objects may remain mutable, so it is not a guarantee of deep immutability.
- Use the example that matches the values, runtime version and dependencies in your application.
PHP 8.2 Readonly Classes: Value Object Patterns is useful when its behavior matches the data structure and lifecycle of your PHP application. Choose it with a clear understanding of its preconditions and returned values.