Cannot use NameSpace\ClassName as ClassName because the name is already in use

18 février 2019 par: Benoit Bonneville

Your Error is : Cannot use NameSpace\ClassName as ClassName because the name is already in use

Situation

Your Code is composed of 3 classes :

MyNamespace/Foo.php – Main Class that cause the problem

namespace MyNamespace;

use OtherNameSpace\ClassName;

class Foo {}

MyNamespace/ClassName.php – ClassName NOT used but in the same Directory/Namespace as Foo

namespace MyNamespace;

class ClassName{}

OtherNameSpace/ClassName.php – ClassName in ANOTHER Namespace, and you need to « use » it into Foo

namespace OtherNameSpace;

class ClassName{}

What’s Happens

When php evaluate your class « Foo » it load the whole namespace « MyNamespace », even the « ClassName ».
But your use declaration ask PhP to load another « ClassName », which as already been loaded.
This is a unwanted behavior from Php OpCache handling, the same Namespace class should not be loaded.

Solution

Upgrade to php 7.0.13 or disable opcache

Details : https://bugs.php.net/bug.php?id=66773
Or Here
https://stackoverflow.com/questions/32928389/name-already-in-use-fatal-error-using-composers-autoload-in-phpunit

Filed under: Non classé

Répondre