website/errors/property.defaultValue.md
<?php declare(strict_types = 1);
class Foo
{
public int $count = 'hello';
}
The default value assigned to a property does not match the property's declared native type. In the example above, the property $count is declared as int, but its default value is the string 'hello'. This would cause a TypeError at runtime.
PHPStan checks that the default value is compatible with the property's type declaration so that the class can be instantiated without errors.
Change the default value to match the property's type:
<?php declare(strict_types = 1);
class Foo
{
- public int $count = 'hello';
+ public int $count = 0;
}
Or change the property type to match the default value:
<?php declare(strict_types = 1);
class Foo
{
- public int $count = 'hello';
+ public string $count = 'hello';
}