One-letter prefix for constants of a class of PHP?

I noticed that many (all?) PHP constants have a single-letter prefix, like E_NOTICE , T_STRING , etc. When defining a set of class constants that work together, do you prefer to follow similar practices, or do you prefer to be more detailed?

 class Foo { // let 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'; } 
+4
source share
7 answers

Prior to 5.3, PHP was limited to one global namespace. The value of any constants declared using define or built into the language required a prefix to break up themselves - posting names for cheap if you do.

About the constants themselves: while E_NOTICE easier to type than ERROR_NOTICE , the first has the main drawback that it is not self-documenting. When in a global context you don’t just have to break constants into a prefix, these prefixes should also be as descriptive as possible.

Class constants are a slightly different beast, because you will always refer to them by the class name - partitioning will be created. Thus, you will get Account::STATUS_CONFIRMED and Account::STATUS_BANNED . But if I planned to have several dozen statuses, I would put them in my class, for example. AccountStatus::CONFIRMED , AccountStatus::BANNED , etc.

No matter what naming convention you accept for constants, the main considerations are separation and self-documenting names (verbosity).

+6
source

For things like constants, I prefer to be verbose. I find this makes it easier to understand the code later (or for people who don't write it), and since I use the IDE, I usually don't have to worry about entering the entire long name when I code.

+2
source

Inside a class, you better not use prefixes at all if they don't make much sense in their contexts. The letter prefix that you see with constants is that in PHP there were no namespaces when they were entered; but classes are some form of namespace, so it really isn’t necessary.

If, however, you intend to use the prefix in any case, I suggest you use the most significant, that is, the full word instead of a single letter. I would not have guessed that I stands for INPUT .

+2
source

I prefer to be more detailed.

 class Bar { const INPUT_STRING = 'string'; const INPUT_INTEGER = 'integer'; const INPUT_FLOAT = 'float'; } 

This makes it easier to read the code for the next person who needs to work with the code. You might consider adding your own frame prefix to the constants (for example: XYZ_INPUT_STRING), which will reduce the conflict change with other components that you can use. The disadvantage of more verbose is that variablenames tend to increase, but a good IDE supports code completion, so you don't have to enter the name of the whole variable.

0
source

The reason most PHP constants are prefixed is because of the namespace, so. For example, in E_USER_ERROR E_ performs two functions:

  • It tells you that the constant is related to the error handling system.
  • This makes it so that if you want to have a constant named USER_ERROR in your application, you can.

In PHP5, we can use class constants, so the namespace is no longer required. See, for example, DATE_ constants . The equivalent of DATE_ATOM is DateTime::ATOM , where DateTime is a PHP class that deals with dates. DATE_ATOM still exists, apparently for the old reason, but this constant was created today, it is simply called DateTime::ATOM .

In your case, it might make sense to define an input class that will have these constants:

 class input { public const STRING='string'; public const INTEGER = 'integer'; public const FLOAT = 'float'; } 
0
source

Unlike C and friends, in dynamic languages ​​we rarely, if ever, need pre-declared constants. because descriptive string values ​​work just as well. There is no real benefit to using

 if($account->status == AccountStatus::CONFIRMED) 

compared with

 if($account->is('confirmed')) 

The latter is more readable and requires less code.

Before you start writing outraged comments, see how jQuery does it: http://api.jquery.com/animate/ . 'hide', 'toggle', 'fast', etc. are actually constants; they simply are not declared as such.

0
source

Prefixes are used for namespace characters. For instance. since constants are declared in the global scope, you can use the prefix to indicate its context and avoid colliding with another use of the character in a different context.

Since class constants already exist in context (class), there is no need to prefix it further. Instead of your example, this will probably make more sense:

 class Input { const STRING = 'string'; const INTEGER = 'integer'; const FLOAT = 'float'; } 

However, I find that constants (class or global) are generally not what I use a lot in PHP. You can usually replace a constant with a more specific method name, and this usually makes the code more convenient to maintain. For example, instead of something like this:

 $foo->addParameter("foo", Input::STRING); 

You can do it:

 $foo->addStringParameter("foo"); 

Easier to read and easier to write.

0
source

Source: https://habr.com/ru/post/1304594/


All Articles