====== PHP RFC: Class Naming ======
* Version: 1.0
* Date: 2017-06-03
* Author: Richard Fussenegger, php@fleshgrinder.com
* Status: Accepted
* First Published at: http://wiki.php.net/rfc/class-naming
===== Introduction =====
The PHP coding standard does not cover how class names should be written. This leads to friction within the userland community that is now largely following the [[http://www.php-fig.org/psr/psr-1/|standard recommendation PSR-1]]. Extending our current coding standard to cover edge cases about abbreviations and acronyms/initialisms would resolve any future discussion.
===== Proposal =====
Extend the coding standard to explicitly specify how abbreviations and acronyms/initialisms are to be handled when writing user-level class names. The current rule is:
> Classes should be given descriptive names. Avoid using abbreviations where possible. Each word in the class name should start with a capital letter, without underscore delimiters (CamelCaps starting with a capital letter). The class name should be prefixed with the name of the 'parent set' (e.g. the name of the extension)::
>
> Good:
> 'Curl'
> 'FooBar'
>
> Bad:
> 'foobar'
> 'foo_bar'
>
> — [[https://github.com/php/php-src/blob/abac7e81dd7b2e851562c60377951da5a5a99e30/CODING_STANDARDS#L154-L166|CODING_STANDARD]]
While it is stated that abbreviations should be avoided, it is silent on what to do if they are used; especially in the case of acronyms/initialisms. There are essentially three choices possible now:
- **PascalCase except Acronyms/Initialisms** — which is how the majority of user-level class names are written, and it matches the approach of many other programming languages.
- **Always PascalCase** — which is basically what [[http://www.php-fig.org/psr/psr-1/|PSR-1]] defines, however, it would make most of the currently existing user-level class names invalid.
- **Do Nothing** — which of course automatically means that any approach is allowed, and the community discussions around this topic will continue.
> **IMPORTANT!**
>
> Regardless of the outcome of this RFC, existing user-level class names are not required to be changed. Although it would be possible (class names are case-insensitive). The reason why renaming is not proposed is simple: this RFC would most probably fail because too many people are against such purely cosmetic changes.
>
> **IMPORTANT!**
==== PascalCase except Acronyms/Initialisms ====
> Class names should be descriptive nouns in PascalCase and as short as possible. Each word in the class name should start with a capital letter, without underscore delimiters. The class name should be prefixed with the name of the "parent set" (e.g. the name of the extension) if no namespaces are used. Abbreviations and acronyms as well as initialisms should be avoided wherever possible, unless they are much more widely used than the long form (e.g. HTTP or URL). Abbreviations start with a capital letter followed by lowercase letters, whereas acronyms and initialisms are written according to their standard notation. Usage of acronyms and initialisms is not allowed if they are not widely adopted and recognized as such.
>
> Good:
> 'Curl'
> 'CurlResponse'
> 'HTTPStatusCode'
> 'URL'
> 'BTreeMap' (B-tree Map)
> 'Id' (Identifier)
> 'ID' (Identity Document)
> 'Char' (Character)
> 'Intl' (Internationalization)
> 'Radar' (Radio Detecting and Ranging)
>
> Bad:
> 'curl'
> 'curl_response'
> 'HttpStatusCode'
> 'Url'
> 'BtreeMap'
> 'ID' (Identifier)
> 'CHAR'
> 'INTL'
> 'RADAR' (Radio Detecting and Ranging)
>
> — [[https://github.com/php/php-src/pull/2552|GitHub PR #2552]]
==== Always PascalCase ====
> Class names should be descriptive nouns in PascalCase and as short as possible. Each word in the class name should start with a capital letter, without underscore delimiters. The class name should be prefixed with the name of the "parent set" (e.g. the name of the extension) if no namespaces are used. Abbreviations and acronyms as well as initialisms should be avoided wherever possible, unless they are much more widely used than the long form (e.g. HTTP or URL). Abbreviations, acronyms, and initialisms follow the same letter-casing as any other word.
>
> Good:
> 'Curl'
> 'CurlResponse'
> 'HttpStatusCode'
> 'Url'
> 'BTreeMap' (B-tree Map)
> 'Id' (Identifier and Identity Document)
> 'Char' (Character)
> 'Intl' (Internationalization)
> 'Radar' (Radio Detecting and Ranging)
>
> Bad:
> 'curl'
> 'curl_response'
> 'HTTPStatusCode'
> 'URL'
> 'BtreeMap'
> 'ID'
> 'CHAR'
> 'INTL'
> 'RADAR'
>
> — [[https://github.com/php/php-src/pull/2553|GitHub PR #2553]]
===== Backward Incompatible Changes =====
None, we only update the coding standards for the future.
===== Future Scope =====
* Update existing user-level class names according to the outcome of this RFC.
===== Proposed Voting Choices =====
Simple 50%+1 majority vote:
* Yes
* No
\\
* PascalCase except Acronyms
* Always PascalCase
===== Patches and Tests =====
* [[https://github.com/php/php-src/pull/2552|GitHub PR #2552: PascalCase except Acronyms]]
* [[https://github.com/php/php-src/pull/2553|GitHub PR #2553: Always PascalCase]]
===== References =====
* [[http://news.php.net/php.internals/99337|Internals Discussion]]
* [[http://news.php.net/php.internals/99241|Internals Pre-Discussion]]