Default value for property of type int may not be null. Use the nullable type ?int to allow null default value

Description

When an argument is typed, and has a null default value, PHP makes it automagically nullable. That is not the case for properties, which yields this error.

Example

<?php

class X {
     private int $i = null;
}

function foo(int $i = null) { }

?>

Solutions

  • Add the nullable type to the type definition.

  • Use another default value, within the current type domain.