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';
}
Up until 5.3 PHP was limited to a single global namespace. Meaning any constants declared with
defineor built into the language required a prefix to partition themselves – namespacing on the cheap, if you will.About constants themselves: while
E_NOTICEis easier to type thanERROR_NOTICEthe 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_CONFIRMEDandAccount::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).