1.2.143. Class, Interface, Enum Or Trait With Identical Names

The following names are used at the same time for classes, interfaces or traits. For example,

Even if they are in different namespaces, identical names makes classes easy to confuse. This is often solved by using alias at import time : this leads to more confusion, as a class suddenly changes its name.

Internally, PHP use the same list for all classes, interfaces and traits. As such, it is not allowed to have both a trait and a class with the same name.

In PHP 4, and PHP 5 before namespaces, it was not possible to have classes with the same name. They were simply included after a check.

<?php
    class a     { /* some definitions */ }
    interface a { /* some definitions */ }
    trait a     { /* some definitions */ }
    enum a      { /* some definitions */ } // PHP 8.1
?>

1.2.143.1. Suggestions

  • Use distinct names for every class, trait and interface.

  • Keep eponymous classes, traits and interfaces in distinct files, for definition but also for usage. When this happens, rename one of them.

1.2.143.2. Specs

Short name

Classes/CitSameName

Rulesets

All, Analyze

Exakat since

0.8.4

PHP Version

All

Severity

Minor

Time To Fix

Quick (30 mins)

Precision

Very high

Features

class

Examples

shopware, NextCloud

Available in

Entreprise Edition, Exakat Cloud