Is there a well-established naming convention for PHP namespaces?
Asked Answered
S

4

40

So far, I've seen many different naming conventions used for PHP namespaces. Some people use PascalCase\Just\Like\For\Classes, some use underscored\lower_case\names, some even use the Java convention for package names: com\domain\project\package.

The question is very simple -- can any of these (or other) conventions be called well-established? Why? Are any of them recommended by authorities like Zend or the developers of well-known PHP frameworks?

Swiger answered 30/11, 2009 at 15:5 Comment(0)
D
29

A PHP Standards Working Group made up of contributors to many different PHP frameworks and projects came up with the following proposal for appropriate namespace usage:

https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md

Damaging answered 14/9, 2010 at 2:14 Comment(1)
This does anything but answer the question. The only mention of capitalization I see is "Alphabetic characters in vendor names, namespaces, and class names may be of any combination of lower case and upper case"Torrid
L
5

I don't think you can say there is anything well established at this point, but there is an RFC discussing it for PEAR2: http://wiki.php.net/pear/rfc/pear2_naming_standards

My take is that namespaces are a form of variables, and therefore should follow the same convention as they do. I genereally prefer lowercase_underscore for free variables (As opposed to object members), so the most natural for my taste would be namespace\ClassName. This also matches the most prevalent conventions from other languages. On the other hand, the same argument could be made for pre-5.3 pseudo-namespaces, but here the de-facto standard seems to be using CamelCase.

Loudish answered 30/11, 2009 at 18:42 Comment(0)
L
1

Personally I like writing classnames upper camelcase, class attributes and methods lower camelcase and class attributes prefixed with an underscore.

Other local variables i'm also writing lower camelcase without an underscore prefix. Object instances are always written uppper camelcase etc. etc. I don't really think that there is a best way, you just have to be consistent to your codingstandard. This gives you the advantage of reading faster through your code and it should give a faster insight in what's happening at which codelines.

Liechtenstein answered 30/11, 2009 at 15:15 Comment(3)
-1 Sharing your personal standard does not contribute in answering this particular question. None of the things you mention makes any reference to sources that support your view. In short: please focus on the question when posting an answer.Moulder
Although you're right, this answer was posted over 7 years ago.Liechtenstein
True it was quite some time ago, I never look at those dates. Amazing if you think about it: the question and its answers still attract visitors and help people learn. SO is such an icon. That is also the reasoning behind my vote: up-votes for answers that are add value to the specific question and down-votes for those who don't. That ensures the quality of SO: only the author can delete (some exceptions), but the community decides. I find it very elegant really. Let me stress this is nothing personal: I saw you post many good quality answers. I just think this is not one of them :-)Moulder
G
0

Some might say that PascalCase except Acronyms/Initialisms is the preferred choice. So, for example, the following are appropriate versions:

  • 'Curl'
  • 'CurlResponse'
  • 'HTTPStatusCode'
  • 'URL'
  • 'BTreeMap' (B-tree Map)
  • 'Id' (Identifier)
  • 'ID' (Identity Document)
  • 'Char' (Character)
  • 'Intl' (Internationalization)
  • 'Radar' (Radio Detecting and Ranging)

Source: At wiki.php.net


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)...

Source: At github.com

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:

  1. 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;

  2. Always PascalCase - Which is basically what PSR-1 defines, however, it would make most of the currently existing user-level class names invalid;

  3. Do Nothing - Which of course automatically means that any approach is allowed, and the community discussions around this topic will continue.

...

"What class naming style should we use?" (final result):

  • PascalCase except Acronyms: 15
  • Always PascalCase: 11

Source: At wiki.php.net


PSR-1

  1. Namespace and Class Names

    ...

    Class names MUST be declared in StudlyCaps.

    ...

Source: At php-fig.org

Gynandromorph answered 29/1, 2023 at 17:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.