是否可以在PHP中将常量命名为受保护的单词? [英] Is it possible to name a constant a protected word in PHP?
问题描述
在C#中,变量和其他名称可以通过在名称前面加上@符号来命名为受保护的名称,例如类".因此,@ class是有效名称.是否可以在PHP中执行相同的操作?我正在使用一类常量来模拟HTML属性(例如ID和Class)的枚举.现在,我正在使用"CssClass",但我宁愿以某种方式使用名称.
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.
推荐答案
不,不可能,至少不是类常量.
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.
我不了解C#,但是PHP中没有任何特殊符号可以将关键字转换为标识符.只要您不使用与关键字完全相同的名称(不区分大小写),它就可以是任何普通的常量名称.
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.
前缀(由于不只是CSS而有所不同)怎么样?可以重复输入,但这是一个不错的解决方法.我知道,如果您的班级被命名为HTMLAttribute
之类的话,这可能也是多余的,但这是最简单的方法.
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
这篇关于是否可以在PHP中将常量命名为受保护的单词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!