1.2.978. Possible Alias Confusion

An alias is used for a class that doesn’t belong to the current namespace, while there is such a class. This also applies to traits and interfaces.

When no alias is used, PHP will search for a class in the local space. Since classes, traits and interfaces are usually stored one per file, it is a valid syntax to create an alias, even if this alias name is the name of a class in the same namespace.

Yet, with an alias referring to a remote class, while a local one is available, it is possible to generate confusion.

<?php

// This should be in a separate file, but has been merged here, for display purposes.
namespace A {
    //an alias from a namespace called C
    use C\A as C_A;

    //an alias from a namespace called C, which will superseed the local A\B class (see below)
    use C\D as B;
}

namespace A {
    // There is a class B in the A namespace
    class B {}
}

?>

1.2.978.1. Suggestions

  • Avoid using existing classes names for alias

  • Use a coding convention to distinguish alias from names

1.2.978.2. Specs

Short name

Namespaces/AliasConfusion

Rulesets

All, Changed Behavior, Semantics, Suggestions

Exakat since

2.1.2

PHP Version

All

Severity

Minor

Time To Fix

Quick (30 mins)

Precision

Very high

Features

semantics

Available in

Entreprise Edition, Exakat Cloud