|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #66300 Change namespace resolution from current namespace to root namespace
Submitted: 2013-12-16 12:54 UTC Modified: 2016-02-21 12:14 UTC
Avg. Score:4.0 ± 0.0
Reproduced:0 of 0 (0.0%)
From: worldoffame at hotmail dot com Assigned:
Status: Wont fix Package: Class/Object related
PHP Version: Irrelevant OS: irrelevant
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Bug Type:
From: worldoffame at hotmail dot com
New email:
PHP Version: OS:


 [2013-12-16 12:54 UTC] worldoffame at hotmail dot com
One major problem I have with PHP namespace is that the namespace resolution starts at the current namespace. This is unheard of, none of the well-known programming languages does this for its namespace implementation(C++, Java, C#, Python, Ruby etc). This mis-feature results in the fact that a leading backslash has to be used on object instantiation unless the class is imported with the use statement, see test script for example. 

I have no problem with PHP's backslashes implementation, but not with the leading backslash. I cant imagine anyone disagreeing that the leading backslash is plainly ugly. Sometimes whether the syntax is ugly or not is subjective, but when everyone agrees with it it becomes objectively true. On the other hand, missing leading backslash is also the source of various bugs that are a pain to detect/fix.

The request is that PHP should change the namespace resolution to the root global namespace so that the leading backslash can be omitted. PHP's built-in classes are automatically imported into every namespace/class, and if the current namespace declares a class that share the same name as the global namespace's class the precedence is given to the class in current namespace. So the code throw new Exception will first search for an Exception class in current namespace, if such a class is not found it will use Exception class from PHP's global namespace. 

I believe this will help lots of programmers really, get rid of the ugly leading backslash will make the code much more readable and beautiful. In fact, PHP's built-in functions are loaded this way, not sure why the resolution rule does not apply for classes. 

Test script:
namespace mynamespace{
    $arrayobject = new ArrayObject;
    $pdo = new PDO($dsn, $user, $password); // assume these variables are globally available to be concise.

Expected result:
The expected result is that the above code should work

Actual result:
Fatal error: Class mynamespace\ArrayObject is undefined... 

// so instead, have to use \ArrayObject and \PDO to get this to work, but leading backslash is ugly.


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2013-12-16 12:58 UTC] worldoffame at hotmail dot com
 [2016-02-21 12:14 UTC]
-Status: Open +Status: Wont fix
 [2016-02-21 12:14 UTC]
At this point, this definitely won't happen (unless its part of some larger proposal, like a new module system).

The primary issue with a global fallback for classes is that it interacts badly with autoloading. Namely, every time you use a global class we'd first have to try loading it in the namespace, again and again.
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Tue Jul 27 10:01:23 2021 UTC