1.2.1261. Unitialized Properties

Properties that are not initialized in the constructor, nor at definition.

With the above class, when m() is accessed right after instantiation, there will be a missing property. Using default values at property definition, or setting default values in the constructor ensures that the created object is consistent.

<?php

class X {
    private $i1 = 1, $i2;
    protected $u1, $u2;

    function __construct() {
        $this->i2 = 1 + $this->u2;
    }

    function m() {
        echo $this->i1, $this->i2, $this->u1, $this->u2;
    }
}
?>

1.2.1261.1. Suggestions

  • Add an explicit initialization for each property.

1.2.1261.2. Specs

Short name

Classes/UnitializedProperties

Rulesets

All, Changed Behavior, Suggestions, Top10

Exakat since

0.8.9

PHP Version

All

Severity

Major

Time To Fix

Quick (30 mins)

Precision

Very high

Features

property

Examples

SPIP

Available in

Entreprise Edition, Exakat Cloud