1.2.253. Could Be Class Constant

When a property is defined, with a default value, then read, but never modified, it could be turned into a constant.

Such a property may initially be intended to have a value update, but that never turned out in the code.

By making the property a constant, it makes visible its constant nature, and reduce the complexity of the code.

<?php

class foo {
    // $this->bar is never modified.
    private $bar = 1;

    // $this->foofoo is modified, at least once
    private $foofoo = 2;

    function method($a) {
        $this->foofoo = $this->bar + $a + $this->foofoo;

        return $this->foofoo;
    }

}

?>

See also Class Constants https://www.php.net/manual/en/language.oop5.constants.php.

1.2.253.1. Suggestions

  • Turn the property into a class constant

1.2.253.2. Specs

Short name

Classes/CouldBeClassConstant

Rulesets

All, Class Review

Exakat since

0.8.4

PHP Version

All

Severity

Minor

Time To Fix

Quick (30 mins)

Precision

Very high

Features

class-constant, visibility

Related rule

Never Called Parameter

Available in

Entreprise Edition, Exakat Cloud