PHP 8.1+ Readonly Properties: Enforce Immutability in Value Objects
PHP 8.1+ readonly properties enforce immutability by allowing assignment only during object construction. This prevents accidental modification of configuration objects, value objects, and DTOs.
15 Feb 2026, 02:12 UTC

The Problem with Mutable Properties
In PHP applications, configuration objects, value objects, and DTOs often need to remain unchanged after creation. However, standard properties can be accidentally reassigned, leading to bugs that are difficult to trace. Consider a database connection configuration that gets modified mid-request—the consequences can be catastrophic.
The Solution: Readonly Properties
PHP 8.1 introduced readonly properties that can only be assigned once, during object construction. Any subsequent attempt to modify them throws an Error. This enforces immutability at the language level, not just by convention.
How It Works: A Practical Example
Here's a configuration class using readonly properties:
<?php
class DatabaseConfig
{
public function __construct(
public readonly string $host,
public readonly int $port,
public readonly string $database,
public readonly string $username,
public readonly string $password
) {}
}
$config = new DatabaseConfig('localhost', 3306, 'myapp', 'user', 'secret');
// This works - assignment in constructor
$config = new DatabaseConfig('otherhost', 3306, 'otherdb', 'user', 'secret');
// This throws Error: Cannot modify readonly property DatabaseConfig::$host
// $config->host = 'malicious-host';
// This also throws Error: Cannot modify readonly property
// $config->port = 5432;
Readonly Classes for Complete Immutability
PHP 8.2 introduced readonly classes, which automatically make all properties, constants, and methods readonly:
<?php
readonly class UserCredentials
{
public function __construct(
public string $email,
public string $apiKey,
public int $expiresAt
) {}
// All methods are implicitly readonly
public function isExpired(): bool
{
return $this->expiresAt < time();
}
}
$creds = new UserCredentials('[contact removed]', 'sk_live_abc123', time() + 86400);
// Cannot modify any property or override methods
Common Mistakes to Avoid
- Attempting reassignment in loops: Readonly properties cannot be modified even inside loops or conditional blocks after construction.
- Partial initialization: All readonly properties must be initialized in the constructor—there's no default value fallback.
- Reflection bypass: Reflection can modify readonly properties, creating security concerns in sensitive contexts like authentication tokens.
Verification Steps
- Check PHP version:
php -v(requires 8.1+ for readonly properties, 8.2+ for readonly classes) - Create a test class with a readonly property and attempt reassignment to verify runtime enforcement
- Test serialization:
serialize()andunserialize()work with properly initialized readonly properties
Limitations and Considerations
Readonly properties cannot be uninitialized—they must receive a value during object construction. This differs from nullable properties where ?string $name = null is valid. Additionally, readonly properties work with inheritance but child constructors must initialize parent readonly properties.