1.2.296. 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.296.1. Connex PHP features¶
1.2.296.1.1. Suggestions¶
Replace the hardcoded namespace with the __NAMESPACE__ constant, and extra separators.
1.2.296.1.2. Specs¶
Short name |
Namespaces/CouldUseMagicConstant |
Rulesets |
|
Exakat since |
2.5.0 |
PHP Version |
All |
Severity |
Minor |
Time To Fix |
Quick (30 mins) |
Precision |
High |
Available in |