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

marked as duplicate by user40980, Dan Pichelman, GlenH7, gnat, Bart van Ingen Schenau Jan 13 '15 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.

  • 3
    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
  • 2
    AS_SOMEONE_FORCED_TO_WORK_IN_C_I_SEE_THIS_CONVENTION_QUITE_OFTEN – Crazy Eddie May 20 '12 at 7:28
  • For PHP standards questions, I highly recommend looking at the PSR set of standards, which addresses this and other style questions. php-fig.org/psr/psr-1 – alttag Jul 9 '16 at 15:06
up vote 179 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:

  • ClassName
  • methodName
  • propertyName
  • function_name (meant for global functions)
  • $variable_name
  • 7
    Nice! A good overview of what the PHP community is using for naming. – TehShrike May 20 '12 at 2:50
  • 4
    Great first time answer, welcome to Programmers! – yannis May 20 '12 at 8:28
  • 3
    niceee :D but Yii is missing :P – Alexa May 20 '12 at 9:20
  • 1
    The problem with this is that some frameworks are far more popular than others. Zend, for example, is one of the most popular frameworks, but uses camelCase variables. To really see what most PHP developers, you would have to weight framework by popularity or simply measure based off of github or something like that. – Nathan Merrill Jul 26 '17 at 20:20
  • 1
    @Adam, regarding the variables, I didn't notice any starting with capital letters. Note that I only skimmed through the code of each library (10 to 20 files in various sections of the library). There may have been some, but I didn't notice any. Feel free to check that out and let us know what you find. :) – programmer Jan 2 at 14:41

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.

  • 2
    I can't be more descriptive for my thoughts. – Seçkin Savaşçı May 19 '12 at 15:17
  • 8
    +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.

There are a number of other reasonable style guides out there as well, as listed here. Most seem to agree that class names should be PascalCased but some, including the MIT style guide, indicate methods should be camelCased.

So, four years after I originally answered this question, I would say go with the accepted answer above.

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.

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

  • ClassesAreLikeThis.
  • variablesAndFunctionsLikeThis
  • UNLESS_THEY_ARE_CONSTANTS
  • andMemberVariablesEndWith_

protected by user40980 Jan 12 '15 at 4:09

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

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.