1.2.294. Could Use Namespace Magic Constant

Use the __NAMESPACE__ magic constant, instead of hardcoding the current namespace. That way, the namespace is easier to read, and it will change with the namespace expression.

<?php

namespace A\B\C {
     class D {}
     $className = 'D';

     // hardcoded namespace, needed to instantiate dynamically the class
     // Don't forget the extra \
print        $fullclassName = '\'.__NAMESPACE__.'\'.$className;
     $object = new $fullclassName;


     // hardcoded namespace, needed to instantiate dynamically the class
     $path = "\A\B\C";
     $fullclassName = $path.$className;
     $object = new $fullclassName;

}
?>

1.2.294.1. Suggestions

  • Replace the hardcoded namespace with the __NAMESPACE__ constant, and extra separators.

1.2.294.2. Specs

Short name

Namespaces/CouldUseMagicConstant

Rulesets

All, Changed Behavior, Suggestions

Exakat since

2.5.0

PHP Version

All

Severity

Minor

Time To Fix

Quick (30 mins)

Precision

High

Features

magic-constant

Available in

Entreprise Edition, Exakat Cloud