94

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

3
  • 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
    Commented May 19, 2012 at 15:22
  • 2
    AS_SOMEONE_FORCED_TO_WORK_IN_C_I_SEE_THIS_CONVENTION_QUITE_OFTEN Commented May 20, 2012 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
    Commented Jul 9, 2016 at 15:06

5 Answers 5

211

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           ║ PascalCase  ║ camelCase  ║ camelCase    ║ lower_case ║ lower_case ║
║ CakePHP          ║ PascalCase  ║ camelCase  ║ camelCase    ║ camelCase  ║ camelCase  ║
║ CodeIgniter      ║ 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             ║ PascalCase  ║ camelCase  ║ camelCase    ║ ---------- ║ ---------- ║
║ Prado            ║ PascalCase  ║ camelCase  ║ Pascal/camel ║ ---------- ║ lower_case ║
║ SimplePie RSS    ║ PascalCase  ║ lower_case ║ lower_case   ║ lower_case ║ lower_case ║
║ Symfony          ║ PascalCase  ║ camelCase  ║ camelCase    ║ camelCase  ║ camelCase  ║
║ Laravel          ║ PascalCase  ║ camelCase  ║ camelCase    ║ camelCase  ║ camelCase  ║
║ Yii 2            ║ PascalCase  ║ camelCase  ║ camelCase    ║ camelCase  ║ camelCase  ║
║ Phalcon          ║ PascalCase  ║ camelCase  ║ camelCase    ║ camelCase  ║ camelCase  ║
║ Slim Framework   ║ PascalCase  ║ camelCase  ║ camelCase    ║ camelCase  ║ camelCase  ║
║ FuelPHP          ║ PascalCase  ║ camelCase  ║ camelCase    ║ camelCase  ║ camelCase  ║
║ PHPPixie         ║ PascalCase  ║ camelCase  ║ camelCase    ║ camelCase  ║ camelCase  ║
║ Laminas Project  ║ PascalCase  ║ camelCase  ║ camelCase    ║ camelCase  ║ camelCase  ║
║ Fat-Free         ║ PascalCase  ║ camelCase  ║ camelCase    ║ camelCase  ║ camelCase  ║
║ Lumen            ║ PascalCase  ║ camelCase  ║ camelCase    ║ camelCase  ║ camelCase  ║
║ Flight           ║ 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
5
  • 2
    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. Commented Jul 26, 2017 at 20:20
  • variables never start with a capital letter in all these frameworks? Even if the reference a class object?
    – Adam
    Commented Dec 28, 2017 at 16:53
  • 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
    Commented Jan 2, 2018 at 14:41
  • @Adam personally, the only place I've seen local variables that start with an uppercase letter is in projects built primarily on Microsoft technologies
    – Jonathan
    Commented Sep 13, 2018 at 13:32
  • Given that you took all the time to create this table. Thanks.
    – ed22
    Commented Oct 14, 2019 at 9:49
24

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.

3
  • 2
    I can't be more descriptive for my thoughts. Commented May 19, 2012 at 15:17
  • 8
    +1 for that last sentence. Commented May 19, 2012 at 15:42
  • Strongly agree with you all. This is, by far, the best answer,. I am giving another +1 for the last sentence as well. Commented Aug 12, 2019 at 19:54
3

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.

0
0

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

  • ClassesAreLikeThis.
  • variablesAndFunctionsLikeThis
  • UNLESS_THEY_ARE_CONSTANTS
  • andMemberVariablesEndWith_
0

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.

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