1.2.1226. Typed Class Constants Usage

Class constants may be typed with the usual types, like a property or an argument.

While it appears to be a paradox to give a type to a structure which as a static value, there are several situations where the type can be enforced:

  • When the class constant is modified in a children class: the children class must use the same type as the parent.

  • When the class constant is build with an expression

  • When the class constant is build with another constant

<?php

class x {
     const int A = 1;
}

?>

See also PHP RFC: Typed class constants and `Why Class Constants Should be Typed <https://tomasvotruba.com/blog/2020/06/22/why-class-constants-should-be-typed>_.

1.2.1226.1. Specs

Short name

Classes/TypedClassConstants

Rulesets

All, CompatibilityPHP53, CompatibilityPHP54, CompatibilityPHP55, CompatibilityPHP56, CompatibilityPHP70, CompatibilityPHP71, CompatibilityPHP72, CompatibilityPHP73, CompatibilityPHP74, CompatibilityPHP80, CompatibilityPHP81, CompatibilityPHP82

Exakat since

2.6.0

PHP Version

With PHP 8.3 and more recent

Severity

Minor

Time To Fix

Quick (30 mins)

Precision

Very high

Features

type

Available in

Entreprise Edition, Exakat Cloud