Treat namespaced names as single token, allow using reserved class names#5720
Treat namespaced names as single token, allow using reserved class names#5720nikic wants to merge 4 commits intophp:masterfrom
Conversation
This was already allowed for implicit alias, allow it for explicit ones as well.
|
Regressions found in top 2k composer packages due to forbidden whitespace in namespaced names: |
|
Hello! I remember this was the only obstacle that impaired a second version of the context sensitive language RFC, but was considered a bigger BC break at the time. After that change and later parser changes to allow classes, interfaces, functions, traits and constants to have any name I believe only a single edge case happened: namespace Namespace; // still not possible
namespace/Foo:class; // due to this alternative namespace resolution construct
Foo:class; // which is equivalent to this anyway xDBut that wouldn't be a big deal, probably. |
|
BTW https://3v4l.org/bOvQJ some constructs re-declared as global functions, could lead to some problems: // troll.php
function echo(){}
function die(){}
function exit(){}
//
echo('bye');
die('now!');
exit('please?!')... but also could help making untestable code more testable xD |
| function echo(){} // not valid | ||
| --EXPECTF-- | ||
| Parse error: syntax error, unexpected 'echo' (T_ECHO), expecting %s in %s on line 9 | ||
| function echo(){} // valid |
There was a problem hiding this comment.
Maybe an error like "Can not redeclare intrinsic PHP construct echo..." makes more sense here? Same for exit, die and print ?
|
@marcioAlmada I'm intentionally allowing all of these, without special cases. All symbols can be used, they just need to referenced though a namespaced name, such as Or is the suggestion here specifically to forbid the function-like language constructs in the global scope only? |
Definitely just function-like language stuff like Or perhaps if declaration is allowed at least don't override PHP |
|
Closing in favor of #5827. |
RFC: https://wiki.php.net/rfc/namespaced_names_as_token
Instead of treating namespaced names a sequence of T_STRING and T_NS_SEPARATOR tokens, treat them as a single token. In particular, this allows the use of reserved keywords inside namespaced names. At the same time allow using reserved keywords as class, function and constant names -- these cannot be directly used, but they can be used inside a namespaced name (e.g. fully qualified).
This is BC breaking, because whitespace between namespace separators is no longer supported, so
Foo \ Baris no longer considered a legal namespaced name. I consider this a feature, because this avoids bugs that I've seen a couple of times, like: