website/errors/property.uninitialized.md
<?php declare(strict_types = 1);
class Foo
{
private string $name;
public function getName(): string
{
return $this->name;
}
}
The class has a typed property that is never assigned a value -- neither as a default value nor in the constructor. Accessing an uninitialized typed property in PHP causes a fatal error (Typed property must not be accessed before initialization).
This rule does not apply to readonly properties, which have their own dedicated checks.
Assign a default value to the property:
class Foo
{
- private string $name;
+ private string $name = '';
}
Or initialize the property in the constructor:
class Foo
{
private string $name;
+ public function __construct(string $name)
+ {
+ $this->name = $name;
+ }
+
public function getName(): string
{
return $this->name;
}
}