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
1  
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
1  
AS_SOMEONE_FORCED_TO_WORK_IN_C_I_SEE_THIS_CONVENTION_QUITE_OFTEN –  Crazy Eddie May 20 '12 at 7:28
add comment

5 Answers

up vote 13 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
1  
Nice! A good overview of what the PHP community is using for naming. –  TehShrike May 20 '12 at 2:50
1  
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
add comment

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
1  
I can't be more descriptive for my thoughts. –  Seçkin Savaşçı May 19 '12 at 15:17
4  
+1 for that last sentence. –  Radu Murzea May 19 '12 at 15:42
add comment

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
add comment

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
add comment

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

  • ClassesAreLikeThis.
  • variablesAndFunctionsLikeThis
  • UNLESS_THEY_ARE_CONSTANTS
  • andMemberVariablesEndWith_
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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