1.2.554. Implicit Nullable Type¶
Argument with default value of null are nullable. It works both with the null
typehint (PHP 8.0), or the ?
operator are not used, setting the default value to null is allowed, and makes the argument nullable.
This works with single types, both classes and scalars; it works with union types but not with intersection types.
This doesn’t happen with properties : they must be defined with the nullable type to accept a ``null``value as default value.
This doesn’t happen with constant, whose value must be explicitely defined.
In PHP 8.4, the implicit nullable type are deprecated. They might be removed in PHP 9.0.
<?php
// explicit nullable parameter $s
function bar(?string $s = null) {
// implicit nullable parameter $s
function foo(string $s = null) {
echo $s ?? 'NULL-value';
}
// both display NULL-value
foo();
foo(null);
?>
See also Nullable types, Type declaration, Deprecate implicit nullable parameters #3535 and PHP RFC: Deprecate implicitly nullable parameter types.
1.2.554.1. Connex PHP features¶
1.2.554.1.1. Suggestions¶
Change the default value to a compatible literal : for example,
string $s = ''
Add the explicit
?
nullable operator, or ``null``with PHP 8.0Remove the default value
1.2.554.1.2. Specs¶
Short name |
Classes/HiddenNullable |
Rulesets |
|
Exakat since |
2.1.0 |
PHP Version |
All |
Severity |
Minor |
Time To Fix |
Quick (30 mins) |
Precision |
Very high |
Available in |