2

Using PSR-4 autoloading with Composer, when I try to test this class:

namespace User;

use User\Contracts\UserId;
use User\Contracts\User as UserContract;

class User implements UserContract
{
    private $id;

    public function __construct(UserId $id)
    {
        $this->id = $id;
    }

    public function getId()
    {
        return $this->id;
    }
}

using PHPUnit:

use User\Contracts\UserId;

class UserTest extends \PHPUnit_Framework_TestCase
{
    public function test_the_identifier_can_be_used_as_string()
    {
        $identifier = m::mock(UserId::class);
        $identifier->shouldReceive('__toString')->once()->andReturn('foo');

        $user = new User($identifier);

        $this->assertSame('foo', (string) $user->getId());
    }
}

I receive this error in the console

PHP Fatal error:  Cannot use User\Contracts\UserId as UserId because the name is already in use in /src/User/User.php on line 5

This is the file/folder structure:

src
`-- User
    |-- Contracts
    |    |-- User.php
    |    `-- UserId.php
    |-- User.php
    `-- UserId.php
tests
`-- User
    |-- UserIdTest.php
    `-- UserTest.php

I've done a lot of searches about this problems, but I've not found any solution about that.

  • PHP Version: 5.6.13-1+deb.sury.org~trusty+3
  • PHPUnit version: 5.0.3
  • OS: Ubuntu 14.04.3 LTS

How can I solve this problem?

2 Answers 2

5

You cannot import a class into a namespace that is already declared inside it with the same alias.

You have \User\UserId and want to import \User\Contracts\UserId into the \User namespace. This would make the shortcut UserId ambigous.

http://php.net/manual/en/language.namespaces.importing.php#117334

You could either use an alias when importing, or directly use a relative path:

namespace User;

class User implements Contracts\User
{
    private $id;

    public function __construct(Contracts\UserId $id) {}
}

Update 2018-03-20:

There is a bug report https://bugs.php.net/bug.php?id=66773 that complains about PHP triggering this error if used without opcode cache, but not otherwise. The reason is that with opcache the compilation is done per file, and it is a reasonable expectation that the file's contents ONLY are considered. However it seems that the error is that existing class definitions from previously loaded files are in the way when importing "conflicting" classes.

It was fixed in the correct way for PHP 7.0.13: Every single file only accounts for itself, and if you desire to import a class and use it by it's shorthand name, you may do so irrespective of any other classes that may exist in the namespace. You simply loose access to these classes - which is irrelevant because your code doesn't use them in the first place. If you'd wanted that changed, you'd have to change the imports.

As the opcache is usually disabled on the command line, you were likely to trigger this bug when using PHPUnit.

2

Try the following:

use User\Contracts\UserId as UserIdContract;

probably you're getting a naming conflict because you're doing

use User\Contracts\UserId;

but you also have UserId in the same namespace of the User class.

So...

namespace User;

use User\Contracts\UserId as UserIdContract;
use User\Contracts\User as UserContract;

class User implements UserContract
{
    private $id;

    public function __construct(UserIdContract $id)
    {
        $this->id = $id;
    }

    public function getId()
    {
        return $this->id;
    }
}

Hope it helps!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.