While programming, everyone comes across these types of casings while defining their variables, constants, functions, classes, etc.
1. lowercase — All letters in lowercase.
2. Capitalized — Word’s first letter is upper case, rest are lower case.
3. UPPERCASE — All letters in upper case, multiple words are separated by an underscore
4. camelCase — First word in lower case, all proceeding words have first letter in upper case. Words have no space between them.
5. PascalCase (or StudlyCaps) — All words have first letter capitalized. Words have no space between them.
6. snake_case — All words in lower case, separated by an underscore.
7. kebab-case — All words in lower case, separated by a hyphen.
8. Train-Case — All words in Capitalized case, separated by a hyphen.
Most programmers are aware of these casing styles. But usually, confusion comes in which one to use in a particular case. Let’s see how PSR (PHP Standard Recommendations) defines use of casings in PHP
- ClassNames should use PascalCase e.g. Student, StudentModel, StudentController.
- Constants should use UPPERCASE e.g. LENGTH, CHAR_LENGTH
- Function names should be in camelCase e.g. getStudents, getStudentName
- Variable names don’t have any recommendations as per PSR.
but IMHO, they look much better in camelCase e.g. $studentCount, $oldStudent which is the de-facto standard in core code of mostly popular bundles and frameworks. - Array indexes are also not covered by PSR but are more understandable in snake_case e.g. $student[‘first_name’] in my IMHO. kebab-case or Train-case should be avoided because these can pose unnecessary complications in templating files as these cases are also used in HTML, e.g. kebab-case is used for CSS class names.
Which case should be used with PHP8’s named argument in your opinion?