Auto-initialization From Boolean

The auto-initialization is the conversion a boolean false or true, to an array, by using the array syntax on it.

When applied to a property, it may be impossible, given the type of that property. The warning message also appears if the type allow it: it is recommended to convert the property to an array before using the array syntax.

PHP code

<?php

class X {
    public bool $property = false;
    public bool|array $property2 = false;
}

$x = new X;
// Fatal error, as type doesn't allow it
$x->property[3] = 2;

// Deprecated error, as type allow it
$x->property2[4] = 5;

?>

Before

PHP Parse error:  syntax error in /codes/autoInitializeArrayFromBool.php on line 4

Parse error: syntax error in /codes/autoInitializeArrayFromBool.php on line 4

After

PHP Fatal error:  Uncaught TypeError: Cannot auto-initialize an array inside property X::$property of type bool in /codes/autoInitializeArrayFromBool.php:8
Stack trace:
#0 {main}
  thrown in /codes/autoInitializeArrayFromBool.php on line 8

Fatal error: Uncaught TypeError: Cannot auto-initialize an array inside property X::$property of type bool in /codes/autoInitializeArrayFromBool.php:8
Stack trace:
#0 {main}
  thrown in /codes/autoInitializeArrayFromBool.php on line 8

PHP version change

This behavior changed in 7.4

Error Messages