Ignas R
Ignas R

Reputation: 3409

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

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?

Upvotes: 41

Views: 20493

Answers (4)

Serious Angel
Serious Angel

Reputation: 1564

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

Upvotes: 0

ramsey
ramsey

Reputation: 637

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

Upvotes: 29

troelskn
troelskn

Reputation: 117567

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.

Upvotes: 6

Ben
Ben

Reputation: 11208

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.

Upvotes: 1

Related Questions