Cannot auto-initialize an array inside property %s::$%s of type %s¶
Description¶
The last command is using the array syntax on the property $property
. In fact, the property is of type bool
.
If this was a variable, this would be tolerated, but it cannot happen on a property, which enforces the types at all times.
Indeed, if the union type bool|array
is used, the automatic conversion to array is reported.
When the type of the property is string
, the array syntax is partially supported: it works with integer index, and fails with strings.
Example¶
<?php
class X {
public bool $property = false;
}
$x = new X;
$x->property[4] = 3;
?>
Literal Examples¶
Cannot auto-initialize an array inside property X::$property of type bool
Cannot auto-initialize an array inside property X::$property of type int
Solutions¶
Convert the type of the property to array.
Create a distinct property, with the array type, to use the array syntax.