1.2.1382. Useless Constructor

Class constructor that have empty bodies are useless. They may be removed, as they are not called.

One edge case is when the class has a parent, and the parent constructor must not be called.

Another edge case is promoted properties: the body of the constructor is still empty, but the parameters hold the definitions of properties. These might be better outside the constructor though.

<?php

class X {
    function __construct() {
        // Do nothing
    }
}

class Y extends X {
    // Useful constructor, as it prevents usage of the parent
    function __construct() {
        // Do nothing
    }
}

?>

1.2.1382.1. Suggestions

  • Remove the constructor

1.2.1382.2. Specs

Short name

Classes/UselessConstructor

Rulesets

All, Analyze, Changed Behavior

Exakat since

0.8.4

PHP Version

All

Severity

Minor

Time To Fix

Slow (1 hour)

Precision

High

Features

constructor

Available in

Entreprise Edition, Exakat Cloud