TIL; PHP nullable and default parameters
function test(int $a, bool $b) {}
test(1, false) // ok
test(1) // ArgumentCountError
test(1, null) // TypeError Argument #2 ($b) must be of type bool, null given
a & b are both required.
function test(int $a, ?bool $b) {}
test(1, false) // ok
test(1) // ArgumentCountError
test(1, null) // ok
b is nullable but always required.
function test(int $a, bool $b = false) {}
test(1, false) // ok
test(1) // ok, $b = false
test(1, null) // TypeError Argument #2 ($b) must be of type bool, null given
b is optional, but not nullable (default value will be false
)
function test(int $a, ?bool $b = false) {}
test(1, false) // ok
test(1) // ok, $b = false
test(1, null) // ok
b is optional and nullable. When not specified on function call, b will be
false
by default. It's value may also be null.