1.2.573. Inherited Class Constant Visibility

Visibility of class constant must be public, even when overwritten.

This was not checked until PHP 8.3, where it is now a Fatal Error. When the interface and the class are defined in different files, the error appears at execution time.

<?php

interface i {
     public const I = 1;
     public const J = 2;
}

class x implements i {
     // This should not be possible
     private const I = 10;
     public const J = 20;
}

?>

1.2.573.1. Suggestions

  • Set the constant visibility in the class to public

  • Remove the visibility of the constant in the class

1.2.573.2. Specs

Short name

Interfaces/InheritedClassConstantVisibility

Rulesets

All, CompatibilityPHP82, CompatibilityPHP83

Exakat since

2.5.3

PHP Version

8.2

Severity

Minor

Time To Fix

Quick (30 mins)

Precision

Very high

Features

visibility, lazy-loading

Available in

Entreprise Edition, Exakat Cloud