Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

There are 3 important naming conventions:

  1. with_underscores
  2. PascalCased
  3. camelCased

Other variants are not important because they are not commonly used.

For variables it seems that the one with underscores is the most used by developers so I'll stick with that. I think it's the same for functions.

But what about class, and method names? Which of these 3 is the most used by developers for such constructs? (personally, it's 3. for methods and 2. for classes)

Please do not post things like "use what you feel is right", because the code I'm writing is API for other developers, and I'd like to adopt the most popular coding style :)

share|improve this question

marked as duplicate by MichaelT, Dan Pichelman, GlenH7, gnat, Bart van Ingen Schenau Jan 13 at 16:29

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

I see that you've tagged your question php, but you should probably make the context in which you're asking more explicit. This question would be easy to answer in, say, Objective-C but impossible to answer in many other languages. I'm not sure whether there's an established standard in PHP, but knowing whether you're limiting your Q to that language will help people answer. –  Caleb May 19 '12 at 15:22

6 Answers 6

up vote 28 down vote accepted

I had the same question about a year ago so I looked at some code myself, here is what I found (constants were ALL_CAPS in every project by the way):

║      PHP Project      ║   Classes   ║  Methods   ║  Properties  ║ Functions  ║ Variables  ║
║ Akelos Framework      ║ PascalCase  ║ camelCase  ║ camelCase    ║ lower_case ║ lower_case ║
║ CakePHP Framework     ║ PascalCase  ║ camelCase  ║ camelCase    ║ camelCase  ║ camelCase  ║
║ CodeIgniter Framework ║ Proper_Case ║ lower_case ║ lower_case   ║ lower_case ║ lower_case ║
║ Concrete5 CMS         ║ PascalCase  ║ camelCase  ║ camelCase    ║ lower_case ║ lower_case ║
║ Doctrine ORM          ║ PascalCase  ║ camelCase  ║ camelCase    ║ camelCase  ║ camelCase  ║
║ Drupal CMS            ║ PascalCase  ║ camelCase  ║ camelCase    ║ lower_case ║ lower_case ║
║ Joomla CMS            ║ PascalCase  ║ camelCase  ║ camelCase    ║ camelCase  ║ camelCase  ║
║ modx CMS              ║ PascalCase  ║ camelCase  ║ camelCase    ║ camelCase  ║ lower_case ║
║ Pear Framework        ║ PascalCase  ║ camelCase  ║ camelCase    ║            ║            ║
║ Prado Framework       ║ PascalCase  ║ camelCase  ║ Pascal/camel ║            ║ lower_case ║
║ SimplePie RSS         ║ PascalCase  ║ lower_case ║ lower_case   ║ lower_case ║ lower_case ║
║ Symfony Framework     ║ PascalCase  ║ camelCase  ║ camelCase    ║ camelCase  ║ camelCase  ║
║ WordPress CMS         ║             ║            ║              ║ lower_case ║ lower_case ║
║ Zend Framework        ║ PascalCase  ║ camelCase  ║ camelCase    ║ camelCase  ║ camelCase  ║

So after looking at all this I decided to go with this:

  • ClassName
  • methodName
  • propertyName
  • function_name (meant for global functions)
  • $variable_name
share|improve this answer
Nice! A good overview of what the PHP community is using for naming. –  TehShrike May 20 '12 at 2:50
Great first time answer, welcome to Programmers! –  Yannis Rizos May 20 '12 at 8:28
niceee :D but Yii is missing :P –  Alexa May 20 '12 at 9:20

There is no such thing as "the most popular coding style", it's strictly a matter of your team's conventions and personal preferences. Since you are targeting developers, you should research popular conventions for your platform and follow the one you feel is more convenient, readable and, well, closer to your personal style. For PHP a popular set of naming conventions is Zend Framework's.

Being consistent with the naming convention you choose is more important than the convention itself.

share|improve this answer
I can't be more descriptive for my thoughts. –  Seçkin Savaşçı May 19 '12 at 15:17
+1 for that last sentence. –  Radu Murzea May 19 '12 at 15:42

While it may not be as ubiquitous as the Java version, following the Apache PHP Style Guide certainly won't hurt. For naming conventions:

Naming: FunctionNamesLike, $localVariableName, $objectVariable, ClassNamesLike, MethodNamesLike, CONSTANTS_LIKE_THIS. Global names (classes, functions, variables, defines) must be prefixed to prevent naming clashes with PHP itself. This approach includes preventing prefixes that clash with PHP or are likely to. Apart from constants, prevent underscores in your names unless you simulate namespaces and are sure you can switch to real namespaces once PHP has them (and of course for object variables).

Following this, class and method names would be PascalCased.

share|improve this answer

The answer to this is language dependent. The one answer that isn't would be to conform to whatever rule your surrounding code uses, and if none, consider picking one and converting to it everywhere applicable.

In javascript, the standard API norm is that class names are TitleCase and method and function names camelCase. People commonly use snake_case for variable names, some using camelCase for variable names holding a function reference, for clarity and code readability.

share|improve this answer

I personally find camelcase to be more natural when coding variables, but because variables in PHP are case sensitive, and it's no fun hunting through code looking for a variable you forgot to make camelcase, I always use snake case for variables.

While its true that the conventions are mutable, coding after someone that has used camelcase on variables, and trying to be sure you always use their convention for THAT variable, can be frustrating.

share|improve this answer

We have this weird system where everything has it's own convention:

  • ClassesAreLikeThis.
  • variablesAndFunctionsLikeThis
  • andMemberVariablesEndWith_
share|improve this answer

protected by MichaelT Jan 12 at 4:09

Thank you for your interest in this question. Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.