php version guide
PHP 8.1 Changes
PHP 8.1 added enums, readonly properties, intersection types, first-class callable syntax, Fibers, and the never return type. Enums and readonly properties are the changes most likely to improve ordinary domain code.
Changes Worth Recognising
- Backed enums model a closed set while still mapping to stored strings or integers.
- Readonly properties can be assigned once from class scope.
- Intersection types require a value to satisfy every listed interface.
- First-class callable syntax such as
trim(...)creates aClosure.
Representative Code
PHP example
<?php
enum OrderStatus: string
{
case Draft = 'draft';
case Paid = 'paid';
}
echo OrderStatus::Paid->value . PHP_EOL;
// Prints:
// paid
Upgrade Review
- Decide how unknown stored enum values are handled during hydration.
- Do not confuse readonly properties with deeply immutable objects.
- Adopt Fibers through a library abstraction unless you are building infrastructure.
Enums can remove repeated string validation, but database and API boundaries still need explicit conversion and failure handling.
Practice
Model a Closed Status Set
Create a string-backed enum for draft, paid, and cancelled. Parse a stored value with tryFrom() and handle an unknown value.