Req #66300 [NEW]: Change namespace resolution from current namespace to root namespace
| From: | worldoffame at hotmail dot com | Date: | Mon, 16 Dec 2013 12:54:17 +0000 |
| Subject: | Req #66300 [NEW]: Change namespace resolution from current namespace to root namespace | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-183353@lists.php.net to get a copy of this message | ||
From: worldoffame at hotmail dot com
Operating system: irrelevant
PHP version: Irrelevant
Package: Class/Object related
Bug Type: Feature/Change Request
Bug description:Change namespace resolution from current namespace to root namespace
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 bug report at https://bugs.php.net/bug.php?id=66300&edit=1
--
Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=66300&r=trysnapshot54
Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=66300&r=trysnapshot55
Try a snapshot (trunk): https://bugs.php.net/fix.php?id=66300&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=66300&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=66300&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=66300&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=66300&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=66300&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=66300&r=support
Expected behavior: https://bugs.php.net/fix.php?id=66300&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=66300&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=66300&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=66300&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=66300&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=66300&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=66300&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=66300&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=66300&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=66300&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=66300&r=mysqlcfg