|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2013-12-16 12:58 UTC] worldoffame at hotmail dot com
[2016-02-21 12:14 UTC] nikic@php.net
-Status: Open
+Status: Wont fix
[2016-02-21 12:14 UTC] nikic@php.net
|
|||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Oct 25 17:00:01 2025 UTC |
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.