Req #66300 [Opn->Wfx]: Change namespace resolution from current namespace to root namespace
| From: | nikic@php.net | Date: | Sun, 21 Feb 2016 12:14:22 +0000 |
| Subject: | Req #66300 [Opn->Wfx]: Change namespace resolution from current namespace to root namespace | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-199366@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=66300&edit=1
ID: 66300
Updated by: nikic@php.net
Reported by: worldoffame at hotmail dot com
Summary: Change namespace resolution from current namespace
to root namespace
-Status: Open
+Status: Wont fix
Type: Feature/Change Request
Package: Class/Object related
Operating System: irrelevant
PHP Version: Irrelevant
Block user comment: N
Private report: N
New Comment:
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.
Previous Comments:
------------------------------------------------------------------------
[2013-12-16 12:58:40] worldoffame at hotmail dot com
The
------------------------------------------------------------------------
[2013-12-16 12:54:16] worldoffame at hotmail dot com
Description:
------------
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.
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=66300&edit=1