Skip to content

MIR1510

PropertyPossiblyUninitialized

A property may not be initialized in all code paths before it is used, which can lead to undefined behavior.

<?php
class User {
public string $name;
public function __construct(bool $condition) {
if ($condition) {
$this->name = 'Alice';
}
// $name may be uninitialized if condition is false
}
public function getName(): string {
return $this->name; // Potentially uninitialized
}
}

Ensure the property is initialized in all code paths:

<?php
class User {
public string $name;
public function __construct(bool $condition) {
if ($condition) {
$this->name = 'Alice';
} else {
$this->name = 'Guest';
}
}
}