objects
enum Protocol: string {
case TCP = 'tcp';
case UDP = 'udp';
}
final class Target {
public function __construct(
public readonly string $host,
public readonly int $port,
) {}
}
That ran under strict types
Promotion
Parameters marked with visibility become properties The constructor body stays empty Less boilerplate, same result
readonly
A readonly property is set once and cannot be reassigned Useful for value objects and configuration passed into a service
enums
Backed enums carry a scalar value
Protocol::TCP->value gives "tcp"
Use enums instead of a bag of string constants
final
final stops inheritance
Prefer it unless extension is a designed part of the API
Next
Failures in 08 errors
Posted