Automatic conversion of false to array is deprecated
Description
The array syntax $variable[index] is only available for arrays and strings. Yet, false is an edge case: PHP used to convert automatically, and silently, the boolean to array, when that syntax was used. This is called autovivification.
The other boolean, true behaves the same way, with a different error message.
This feature is used to initialize variables with false, and then convert it to array as needed.
Since PHP 8.1, it is a deprecated behavior.
Example
<?php
$variable = false;
$variable[0] = 1;
// $variable is now an array
class X {
public bool|array $property = false;
}
$x = new X;
$x->property['d'] = 3;
?>
Alternatives
- Use an empty array as default value.
- Test the type of the variable before using it as an array.
Related error messages
- cannot-unset-offset-in-a-non-array-variable
- cannot-auto-initialize-an-array-inside-property-%s::$%s-of-type-%s
Changed Behavior
This error may appear following an evolution in behavior, in previous versions. See autoInitializeArrayFromBool