1.2.1448. Wrong Type With Default

The default value is not of the declared type.

For properties, this will generate an error as soon as the default value is used : this is before constructor call for properties, and when the argument is omitted for promoted properties.

For parameters, the error happens when the argument is omitted, and the default value is fetched. Otherwise, it won’t happen. This error is immediately detected when a literal value is used. It only happens when the default is a constant (class or global) or an expression, as those are only solved at execution time.

<?php

const A = 1;

class B {
    private string $c = A;
}

new B;
//Cannot assign string to property B::$c of type string
?>

See also When does PHP check for Fatal error.

1.2.1448.1. Specs

Short name

Typehints/WrongTypeWithDefault

Rulesets

All, Analyze, Class Review, LintButWontExec

Exakat since

2.4.4

PHP Version

All

Severity

Minor

Time To Fix

Quick (30 mins)

Precision

High

Note

This issue may lint but will not run

Available in

Entreprise Edition, Exakat Cloud