1.2.102. Can’t Overwrite Final Constant

A class constant may be final, and can’t be overwritten in a child class. final is a way to make sure a constant cannot be changed in children classes.

private constants can’t be made final, as they are not accessible to any other class.

<?php

class y extends x {
    const F = 1;
    const P = 2;
}

class x {
    final const F = 3;
    private const PRI = 5; // Private can't be final
    const P = 4;
}

?>

1.2.102.1. Suggestions

  • Remove the final keyword in the parent class

  • Remove the class constant in the child class

  • Rename the class constant in the child class

1.2.102.2. Specs

Short name

Classes/CantOverwriteFinalConstant

Rulesets

All, Analyze, Class Review, LintButWontExec

Exakat since

2.3.9

PHP Version

With PHP 8.1 and more recent

Severity

Major

Time To Fix

Slow (1 hour)

Precision

High

Features

final, overwrite

Note

This issue may lint but will not run

Available in

Entreprise Edition, Exakat Cloud