Unicode identifiers (function names) for non-localization purposes advisable?

前端 未结 2 568
别跟我提以往
别跟我提以往 2020-12-17 17:36

PHP allows Unicode identifiers for variables, functions, classes and constants anyhow. It was certainly intended for localized applications. Wether it\'s a good idea to code

相关标签:
2条回答
  • 2020-12-17 18:33

    Which symbol makes sense as separator for mixed-in parameters in a virtual function name.

    \u2639?

    But other than localization and amusement and decorative effects, which uses of Unicode identifiers are advisable?

    The biggest hurdle after font support is going to be making the character one that can be typed. Outside of a macro or copy/paste, unicode characters are not spectacularly easy to enter. Forcing this upon others is very likely going to violate the "assume the people that work with your code after you are murderous psychopaths that know where you live" rule.

    We use unicode characters in only a few comments in our codebase, like

    // Even though this is the end of the file and we should get an implicit exit, 
    // if we don't actually expressly exit here, PHP segfaults.
    // ♫ Oh, PHP, I love you. ♫
    

    I think that falls into the "amusement and decorative" category. Or the "shoot self in head after slaughtering the php-internals team" category. Pick one.

    Anyway, this is not a good idea because it's going to make your code hard to modify.

    0 讨论(0)
  • 2020-12-17 18:34

    Just to make it clear: PHP does not support Unicode. And it doesn't support Unicode labels. To be more precise PHP defines a LABEL as [a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*. As you can see here, it allows only a small range of characters apart from the typical alphanumeric + underscore. The fact that your Unicode labels are still accepted is only an artifact from the fact, that PHP doesn't have Unicode support. Your special characters are several bytes long in UTF-8 and PHP treats each of these bytes as a separate character and accidentally - with the characters you tried - each of them matched with the \x7f-\xff range mentioned above.

    Further reading on that topic: Exotic names for methods, constants, variables and fields - Bug or Feature?

    0 讨论(0)
提交回复
热议问题