This is an old revision of the document!
PHP RFC: Casing of acronyms in class and method names
- Version: 1.0
- Date: 2024-04-05
- Author: Tim Düsterhus, timwolla@php.net
- Status: Draft (or Under Discussion or Accepted or Declined)
- First Published at: http://wiki.php.net/rfc/class-naming-acronyms
Introduction
The results of the Class Naming RFC decided that class names should be written in PascalCase (“Upper Camel Case”), with the exception of acronyms, which should be included in UPPERCASE. This RFC proposes to revisit the prior decision, instead treating acronyms like regular words, making classnames consistent PascalCase.
Proposal
Update to the Policy
The class naming policy should be updated to the following, with changes highlighted:
Method names follow the studlyCaps (also referred to as bumpy case or camel caps) naming convention, with care taken to minimize the letter count. The initial letter of the name is lowercase, and each letter that starts a new word is capitalized.
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. Abbreviations, acronyms, and initialisms should be treated like regular words, thus they should be written with an uppercase first character, followed by lowercase characters. Usage of acronyms and initialisms is not allowed if they are not widely adopted and recognized as such.
Diverging from this policy is allowed to keep internal consistency within a single extension, if the name follows an established, language-agnostic standard, or for other reasons, if those reasons are properly justified and voted on as part of the RFC process.
Examples
Good method names:
connect() getData() buildSomeWidget() performHttpRequest()
Bad method names:
get_Data() buildsomewidget() getI() performHTTPRequest()
Good class names:
Curl CurlResponse HttpStatusCode Url BtreeMap // B-tree Map Id // Identifier ID // Identity Document Char // Character Intl // Internationalization
Bad class names:
curl curl_response HTTPStatusCode URL BTreeMap ID // Identifier CHAR INTL
Adjusting class names added in PHP 8.4
The DOM HTML5 parsing and serialization RFC introduced new class names and method names that violate the updated naming policy, should this RFC be accepted. As part of this RFC, the following changes will be made:
- The
DOM
namespace will be renamed toDom
, affecting everything contained within it. - DOM\DTDNamedNodeMap -> Dom\DtdNamedNodeMap
- DOM\HTMLCollection -> Dom\HTMLCollection (Justification: The DOM Standard specifies the name)
- DOM\CDATASection -> Dom\CDATASection (Justification: The DOM Standard specifies the name)
- DOM\HTMLDocument -> Dom\HtmlDocument
- DOM\XMLDocument -> Dom\XmlDocument
- DOM\XPath -> Dom\XPath (Justification: The DOM Standard specifies the XPath interfaces that use this casing)
- DOM\Implementation::createHTMLDocument() -> Dom\Implementation::createHtmlDocument()
- DOM\Node::lookupNamespaceURI() -> Dom\Node::lookupNamespaceURI() (Justification: The DOM Standard specifies the name)
- DOM\*::*NS() -> Dom\*::*NS() (Justification: The DOM Standard generally specifies the NS suffix in this casing)
- DOM\DocumentFragment::appendXML() -> Dom\DocumentFragment::appendXml()
- DOM\Document::createCDATASection() -> Dom\Document::createCDATASection() (Justification: The DOM Standard specifies the name)
- DOM\Document::relaxNGValidate() -> Dom\Document::relaxNgValidate()
- DOM\Document::relaxNGValidateSource() -> Dom\Document::relaxNgValidateSource()
- DOM\HTMLDocument::saveXML() -> Dom\HtmlDocument::saveXml()
- DOM\HTMLDocument::saveXMLFile() -> Dom\HtmlDocument::saveXmlFile()
- DOM\HTMLDocument::saveHTML() -> Dom\HtmlDocument::saveHtml()
- DOM\HTMLDocument::saveHTMLFile() -> Dom\HtmlDocument::saveHtmlFile()
- DOM\XMLDocument::saveXML() -> Dom\XmlDocument::saveXml()
- DOM\XMLDocument::saveXMLFile() -> Dom\XmlDocument::saveXmlFile()
For the Improve callbacks in ext/dom and ext/xsl RFC the following changes will be made:
- \DOMXPath::registerPhpFunctionNS() -> \DOMXPath::registerPhpFunctionNS() (Justification: Consistency with the *NS() methods in ext/dom)
- \DOM\XPath::registerPhpFunctionNS() -> \Dom\XPath::registerPhpFunctionNS() (Justification: see above)
- \XSLTProcessor::registerPHPFunctionNS() -> \XSLTProcessor::registerPHPFunctionNS() (Justification: Consistency with \XSLTProcessor::registerPHPFunctions(), which unfortunately is inconsistent with \DOMXPath).
Backward Incompatible Changes
Existing classnames in released versions are not affected, thus there are not backwards incompatible changes. This is a policy RFC.
Proposed PHP Version(s)
Next PHP 8.x (PHP 8.4).
RFC Impact
To SAPIs
SAPIs that expose classes should take the RFC results into account.
To Existing Extensions
Extensions should take the RFC results into account.
To Opcache
None.
New Constants
None.
php.ini Defaults
None.
Open Issues
None.
Unaffected PHP Functionality
Everything that is not a class name of an internal class. Everything that is already in PHP.
Future Scope
None.
Proposed Voting Choices
Include these so readers know where you are heading and can discuss the proposed voting options.
Patches and Tests
None, yet. This RFC will result in changes to:
Implementation
n/a
References
- Prior mailing list discussion: https://externals.io/message/120959#120959
Rejected Features
n/a