Is it possible to name a constant a protected word in PHP?

谁说我不能喝 提交于 2019-12-11 07:34:53

问题


In C#, variables and other things can be named protected names such as "class" by prepending the name with an @ sign. So, @class is a valid name. Is it possible to do this same thing in PHP? I am using a class of constants to simulate an enum for HTML attributes such as ID, and Class. For now I am using "CssClass" but I'd rather use the name Class somehow.


回答1:


Nope, not possible, at least not for class constants.

You cannot use any of the following [reserved] words as constants, class names, function or method names.

I don't know about C#, but there isn't any special symbol in PHP to transform a keyword into an identifier. As long as you don't name it exactly the same as a keyword (barring letter case), it'll just be any normal constant name.

How about a (different since it's not just CSS) prefix? Gets repetitive to type, but is a nice workaround. I realize this may be redundant as well if your class is named something like HTMLAttribute, but it's the easiest way out.

const A_ID = 'id';
const A_CLASS = 'class';
// etc



回答2:


Yes, it is possible.

In fact you can define anything as constant:

define("define", 1);
define("class", 1);
define("if", 1);
define("=.+*", 1);

However, you can not use all defined constants.

You can query them with constant("if") again. But this is not exactly what you asked for. So unlike C# there is no shortcut to use any random constant. But as for naming them, there are almost no restrictions. (Might be a bug though. It's PHP.)




回答3:


Constants:

The name of a constant follows the same rules as any label in PHP. A valid constant name starts with a letter or underscore, followed by any number of letters, numbers, or underscores. As a regular expression, it would be expressed thusly: [a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*

List of reserved keywords:

These words have special meaning in PHP. Some of them represent things which look like functions, some look like constants, and so on--but they're not, really: they are language constructs. You cannot use any of the following words as constants, class names, function or method name.

[see list here]

Within these rules you're free to make up your names. So, for instance, you could name a constant _CLASS, but not CLASS. I'd avoid the use of such ambiguous names though and namespace constants that are particular to the app, like MYAPP_CLASS.




回答4:


Going from PHP5 to PHP7, a class constant could be named almost anything:

class ReservedWord
{
    // Works in PHP >= 7.0 only
    const NULL = null;
    const TRUE = true;
}

However, thanks to this part of the manual and this comment, I've found that a class constant cannot be named these few things (see the test here):

  1. class
  2. static
  3. __halt_compiler (oh, that was so useful!)

Edit: As I found in here in an RFC, the reason why class constant does not work is the name resolution ::class. However, still no idea about the two others.



来源:https://stackoverflow.com/questions/4150761/is-it-possible-to-name-a-constant-a-protected-word-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!