PHP 类常量的单字母前缀?
我注意到许多(全部?)PHP 常量都有一个单字母前缀,例如 E_NOTICE
、T_STRING
等。在定义一组协同工作的类常量时彼此之间,您更喜欢遵循类似的做法,还是更喜欢更冗长的做法?
class Foo {
// let's say 'I' means "input" or some other relevant word
const I_STRING = 'string';
const I_INTEGER = 'integer';
const I_FLOAT = 'float';
}
或者
class Bar {
const INPUT_STRING = 'string';
const INPUT_INTEGER = 'integer';
const INPUT_FLOAT = 'float';
}
I've noticed many (all?) PHP constants have a single-letter prefix, like E_NOTICE
, T_STRING
, etc. When defining a set of class constants that work in conjunction with one another, do you prefer to follow similar practice, or do you prefer to be more verbose?
class Foo {
// let's say 'I' means "input" or some other relevant word
const I_STRING = 'string';
const I_INTEGER = 'integer';
const I_FLOAT = 'float';
}
or
class Bar {
const INPUT_STRING = 'string';
const INPUT_INTEGER = 'integer';
const INPUT_FLOAT = 'float';
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
在 5.3 之前,PHP 仅限于单个全局名称空间。这意味着用
define
声明的或内置到语言中的任何常量都需要一个前缀来对自身进行分区 - 如果您愿意的话,命名空间很便宜。关于常量本身:虽然
E_NOTICE
比ERROR_NOTICE
更容易输入,但前者的主要缺点是不能自记录。在全局上下文中,您不仅需要按前缀对常量进行分区,这些前缀还应该尽可能具有描述性。类常量是一个稍微不同的野兽,因为您将始终通过类名引用它们 - 将内置分区。因此您最终将得到
Account::STATUS_CONFIRMED
和Account: :STATUS_BANNED
。但是,如果我计划有几十个状态,我会将它们放在自己的类中,例如AccountStatus::CONFIRMED
、AccountStatus::BANNED
等。无论命名约定如何您决定使用常量时,主要考虑因素是分区和自记录名称(详细程度)。
Up until 5.3 PHP was limited to a single global namespace. Meaning any constants declared with
define
or built into the language required a prefix to partition themselves - namespacing on the cheap, if you will.About constants themselves: while
E_NOTICE
is easier to type thanERROR_NOTICE
the former has the major disadvantage of not being self-documenting. When in a global context not only do you need to partition constants out by prefix, these prefixes should also be as descriptive as possible.Class constants are a slightly different beast, as you'll always be referencing them by class name - partitioning will be built in. So you'll end up with
Account::STATUS_CONFIRMED
andAccount::STATUS_BANNED
. But if I planned on having several dozen statuses, I'd put these in their own class, e.g.AccountStatus::CONFIRMED
,AccountStatus::BANNED
, etc.Whatever naming convention you decide on for constants the major considerations are partitioning and self-documenting names (verbosity).
对于像常量这样的东西,我更喜欢冗长。我发现它使代码以后更容易理解(或者对于那些没有编写代码的人来说),而且由于我使用 IDE,所以在编码时我通常不必担心输入整个长名称。
For things like constants I prefer to be verbose. I find it makes the code easier to comprehend later (or for folks who didn't write it), and since I use an IDE, I don't usually have to worry about typing the whole long name out when I'm coding.
在类中,最好根本不使用前缀,除非它们在上下文中非常有意义。您在常量中看到的字母前缀是因为 PHP 中引入名称空间时还没有名称空间;但类是某种形式的命名空间,所以这并不是真正必要的。
但是,如果您无论如何都要使用前缀,我建议您使用最有意义的前缀,即完整的单词而不是单个字母。我自己不会猜到
I
的意思是INPUT
。Inside a class, you're better off not using prefixes at all, unless they're extremely meaningful in their contexts. The letter prefix you see with constants is because there were no namespaces in PHP when they were introduced; but classes are some form of namespacing, so this isn't really necessary.
If you are, however, going to use a prefix anyways, I suggest you use the most meaningful one—that is, the full word instead of a single letter. I wouldn't have guessed myself that
I
meantINPUT
.大多数 PHP 常量具有前缀的原因是为了命名它们,以便 .例如,在
E_USER_ERROR
中,E_
做了两件事:USER_ERROR
。在PHP5中,我们可以使用类常量,因此不再需要命名空间。例如,请参阅
DATE_
常量。DATE_ATOM
的等价物是DateTime::ATOM
,其中DateTime
是处理日期的 PHP 类。DATE_ATOM
仍然存在,可能是出于遗留原因,但如果今天创建该常量,它将简单地称为DateTime::ATOM
。在您的情况下,定义一个具有以下常量的输入类可能是有意义的:
The reason most PHP constants have the prefix is to namespace them, so that . For example, in
E_USER_ERROR
, theE_
does two things:USER_ERROR
in your application, you can.In PHP5, we can use class constants, so namespacing is no longer necessary. See for example the
DATE_
constants. The equivalent ofDATE_ATOM
isDateTime::ATOM
, whereDateTime
is the PHP class that deals with dates.DATE_ATOM
still exists, presumably for legacy reason, but were that constant created today, it would simply be calledDateTime::ATOM
.In your case, it might make sense to define an input class that would have these constants:
我更喜欢更详细,
这使得下一个必须处理代码的人更容易阅读代码。您可以考虑将自己的框架前缀添加到常量中(例如:XYZ_INPUT_STRING),这将减少与您可能使用的其他组件发生冲突的变化。更冗长的缺点是变量名往往会变得更长,但是一个好的 IDE 支持代码完成,因此您不必键入整个变量名。
I prefer to be more verbose
This makes the code easier to read for the next person who has to work on the code. You could consider adding your own framework-prefix to the constants (eg.: XYZ_INPUT_STRING) that'll decrease the change of having a conflict with other components that you might use. A downside of being more verbose is that variablenames tend to get longer, but a good IDE supports code completion so you don't have to type the whole variable name.
前缀用于命名空间符号。例如。由于常量是在全局范围内声明的,因此您可以使用前缀来指定其上下文,并避免与不同上下文中该符号的其他使用发生冲突。
由于类常量已经存在于上下文(类)中,因此实际上没有必要进一步为其添加前缀。与您的示例不同,这可能更有意义:
也就是说,我发现常量(类或全局)通常不是我在 PHP 中经常使用的东西。您通常可以用更具体的方法名替换常量,这通常会使代码更易于维护。例如,
您可以这样做
:更容易阅读并且更容易编写。
Prefixes are used to namespace symbols. Eg. since constants are declared in the global scope, you can use a prefix to specify the context of it and avoid clashing with other uses of the symbol in a different context.
Since class constants already exists in a context (the class), there is really no need to prefix it further. Instead of your example, this would probably make more sense:
That said, I find that constants (class or global) are generally not something I use very much in PHP. You can usually replace a constant with a more specific methodname, and this generally make the code much more maintainable. For example, instead of something like this:
You could do this:
It's easier to read and it's easier to write.
与 C 及其类似语言不同,在动态语言中,我们很少(如果有的话)需要预先声明的常量。因为描述性字符串值同样有效。 使用并没有什么真正的好处,
相比之下
后者更具可读性并且需要更少的代码。
在你开始写愤怒的评论之前,看看 jQuery 是如何做到的: http://api.jquery.com/animate / 。 'hide'、'toggle'、'fast' 等实际上是常量,它们只是没有这样声明。
Unlike C and friends, in dynamic languages we rarely, if ever, need pre-declared constants. because descriptive string values work just as fine. There is no real benefit of using
compared to
The latter is more readable and requires less code.
Before you start writing an indignant comment, look how jQuery does it: http://api.jquery.com/animate/ . 'hide', 'toggle', 'fast' etc are actually constants, they are simply not declared as such.