views:

110

answers:

3

The PHP (PEAR, Zend, etc) coding standard specifies that class names should be in upper CamelCase, while methods and variables (associative arrays keys probably as well) should be in lower camelCase format. The rationale for having classes in CamelCase and underscores is to emulates namespaces and create a parallel with file location, but somewhat I find that where it works well for classes it doesn't so much for variables.

Lets say you have to map some variables to your database columns or form elements. MySQL is not case sensitive (that I know of), neither is html, therefore $data['userId'] is pretty much the same as $data['userid'] to these. ORMs like Doctrine actually ignore camelCasing for columns and just fetch them in lowercase (or is it mysql just returning them as such). Nobody wants to deal with data that may or may not look like $data['productQuantity'] or $data['productquantity'] depending on where it's been. $data['product_quantity'] on the other hand leaves little ambiguity.

So what is the reasoning behind the current variable naming scheme of the coding standard? What prevents PHP to amend this standard to say that all variables should be in lowercase separated by an underscore (it works since we know that class names should start with an uppercase letter)?

Edit:

Please note that I'm not asking what is the purpose of a standard. I know what they are meant to resolve in general. I'm asking why the variable name standard in PHP is the way it is. i.e. I understand the reasoning behind the class naming scheme and the benefits in adopting it (makes your library compatible with many autoloader out there, etc). But I don't get why camelCasing variable names. Is there a special reason? Can I break away from this with no consequences?

A: 

It's just a convention... Like a programming social habit

TiuTalk
+2  A: 

So what is the reasoning behind the variable naming scheme of the coding standard?

It makes code:

  • Consistent
  • Easily understandable for others at first glance what a variable is
  • All developers follow the same principles making life much more easier.
Sarfraz
That's the rationale behind most standards in general. I'm asking for the rationale behind the specific naming scheme for variable names. My second paragraph explains that another naming scheme using all lowercase and underscores for variable names is much more compatible with common database naming scheme (i.e. userId vs user_id). What is a good reason for having this particular scheme and not adopt the one I mentioned?
mike
@michael: as far as i am concerned, i don't waste time researching such things, i can not change, won't benefit me, but i still have to go with them :(
Sarfraz
+1  A: 

PHP was strongly influenced by Java, so it was natural to pick up the naming schemes from there. I'm pretty sure that if PHP was invented today, people would have used a different naming scheme. Now it's pretty much standard though, so changing it isn't really an option.

Not though, that this is a de-facto standard. The PHP language doesn't come with any naming guide. It's mainly driven by various frameworks. (Most notably, PEAR and Zend Framework)

troelskn