website/errors/class.nameInUse.md
<?php declare(strict_types = 1);
namespace SomeNamespace;
use SomeOtherNamespace\Foo;
class Foo
{
}
A class is being declared with a name that is already in use within the same namespace. This happens when a use statement imports a class with the same name as a class being declared in the current file. PHP cannot resolve which Foo is intended, resulting in a fatal error at runtime.
In the example above, Foo is imported via use SomeOtherNamespace\Foo and then a class named Foo is declared in the SomeNamespace namespace. Both refer to the short name Foo, creating a conflict.
Rename the local class to avoid the conflict:
<?php declare(strict_types = 1);
namespace SomeNamespace;
use SomeOtherNamespace\Foo;
-class Foo
+class MyFoo
{
}
Or use an alias for the imported class:
<?php declare(strict_types = 1);
namespace SomeNamespace;
-use SomeOtherNamespace\Foo;
+use SomeOtherNamespace\Foo as OtherFoo;
class Foo
{
}
Or remove the use statement if the imported class is not needed:
<?php declare(strict_types = 1);
namespace SomeNamespace;
-use SomeOtherNamespace\Foo;
-
class Foo
{
}