php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #71983 Not clear namespace resolution
Submitted: 2016-04-07 16:03 UTC Modified: 2016-04-09 14:24 UTC
From: goetas at gmail dot com Assigned: cmb (profile)
Status: Not a bug Package: *General Issues
PHP Version: 5.6.20 OS:
Private report: No CVE-ID: None
 [2016-04-07 16:03 UTC] goetas at gmail dot com
Description:
------------
It is not possible to alias a class from a namespace.

I have also tried placing classes in different files, but the problem persists.

Test script:
---------------
<?php
namespace A {
    class C {
    }
}
namespace B {
    class C {
    }
}

namespace B {
    use A\C;
    echo "something";
}

Expected result:
----------------
something

Actual result:
--------------
PHP Fatal error:  Cannot use A\C as C because the name is already in use in /vagrant/A/run.php on line 12

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-04-09 11:25 UTC] cmb@php.net
-Status: Open +Status: Not a bug -Assigned To: +Assigned To: cmb
 [2016-04-09 11:25 UTC] cmb@php.net
The second namespace declaration already defines a class C in
namespace B, hence the error. You'd have to alias A\C, see
<https://3v4l.org/g90NX>, for instance.
 [2016-04-09 12:04 UTC] goetas at gmail dot com
Ok, Thanks. It is clear now. Buy the documentation says that aliases are per file. But now it seems that they are per namespace. Is it?
 [2016-04-09 14:24 UTC] cmb@php.net
> Buy the documentation says that aliases are per file. But now it
> seems that they are per namespace. Is it?

You're probably referring to [1]:

> Importing rules are per file basis, meaning included files will
> NOT inherit the parent file's importing rules.

And indeed, that is so. However, if you are importing a class
inside a namespace (as in your supplied test script), there
must not already be another class with the same name in
that namespace.

For further information on this issue, please ask on
php-general@lists.php.net.

[1] <http://php.net/manual/en/language.namespaces.importing.php#language.namespaces.importing.scope>
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Dec 27 00:01:30 2024 UTC