The readonly Trap: Why Task[] Fails in PHP 8.3
If you are upgrading to PHP 8.3 and attempting to define a property like private Task[] $tasks;, you will encounter a hard syntax error immediately upon parsing the file. The interpreter throws PHP Parse error: syntax error, unexpected token "[", expecting variable. This behavior is not a bug in your IDE or a configuration oversight; it is a deliberate language design decision rooted in how PHP 8.3 handles class visibility modifiers.
In previous versions of PHP (8.1 and earlier), the syntax $class[] was permitted for properties, allowing you to declare an array of instances directly on the class definition. However, starting with PHP 8.2, and strictly enforced in 8.3, this specific combinationâdeclaring a property as both typed and pluralizedârequires a readonly modifier.
The restriction exists because of how the engine initializes properties during object construction. When a property is declared without the readonly keyword, PHP expects to assign a value to it at runtime, typically within the constructor. The syntax $tasks[] implies an expansion operation that conflicts with the static type checking logic introduced in the 8.x series regarding non-readonly properties.
To resolve this, you must explicitly mark the property as readonly. Here is the correct implementation for PHP 8.3:
class TaskRepository {
// This now works in PHP 8.3+
private readonly Task[] $tasks;
}
By adding readonly, you signal to the engine that this property will be initialized once and never modified afterwards via assignment. While the readonly keyword restricts mutability, it fully supports array type hints. This allows you to pass an existing collection of Task objects into the constructor:
function __construct(array $tasks) {
// Assign the whole array directly
$this->tasks = $tasks;
}
This approach maintains strict typing while adhering to the new language rules. It ensures that your codebase remains type-safe without sacrificing the ability to group objects into a collection.
The Trade-Off: Immutability vs. Flexibility
Adopting readonly properties for collections introduces an implicit contract of immutability. Once the object is instantiated, the $tasks array cannot be reassigned. If your use case requires dynamically adding or removing tasks after instantiation, you must ensure that any mutation happens via methods rather than direct property assignment.
// â This will fail at runtime if $tasks is readonly
$this->tasks[] = new Task('new task');
// This is the correct pattern for readonly arrays
public function addTask(Task $task): void {
$this->tasks[] = $task;
}
If your architecture relies heavily on mutable state stored directly in properties, this shift requires a mental adjustment. You are effectively forcing a separation between data storage and data modification logic. For many modern PHP applications, particularly those using Laravel or similar frameworks, this pattern aligns well with value object principles and helps prevent accidental side effects.
Why array Was the Workaround
Your current workaround of using private array $tasks; is valid but loses specific type information. While it allows you to store an array of Task objects, the type checker cannot guarantee that only Task instances are present. This opens the door to passing unrelated arrays or mixed types into the property, which defeats the purpose of strong typing.
Using readonly Task[] provides two distinct benefits:
- Compile-time safety: The IDE and static analyzers can verify that any value assigned to
$tasksis a collection ofTaskobjects. - Explicit intent: It clearly communicates that this specific property holds a homogeneous collection, not just a generic list of values.
Conclusion on Syntax Evolution
The removal of the bare ClassName[] syntax for non-readonly properties in PHP 8.3 is a step toward stricter initialization rules. It eliminates ambiguity regarding when an array is initialized versus when it is appended to. By requiring readonly, the language forces developers to think about the lifecycle of their collections more deliberately.
If you are maintaining legacy code on PHP 8.2 or earlier, you can still use private Task[] $tasks; without the readonly modifier. However, for new development on PHP 8.3+, the readonly keyword is no longer optional if you wish to use array type hints on properties. For more details on how this interacts with dependency injection strategies in modern frameworks, check out resources from https://laravelcompany.com.
