Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP/MySQL naming conventions: camelCase vs under_score?

Quite often in PHP model code (at least in my own such code) there are direct references to MySQL table and field names, and since MySQL identifiers are for the most part case-insensitive I typically use the under_score naming convention to make those identifiers a bit more readable.

At the same time however, it seems that most folks use camelCase conventions when they create PHP class libraries, and I've been trying to do that, too.

On top of that, the PHP built-in functions themselves are inconsistent. Some of them use camelCase, others use under_scores, and others use C-style naming (for example "strtolower").

The result is that the code tends to be much less readable than I prefer, what with mixed camelCase, under_score, and C-style naming conventions showing up quite near each other in the code.

How are other folks dealing with this? Perhaps there's some way people have found to organize their work so that the different naming conventions tend not to appear quite so close to each other? Or perhaps there are class libraries that if used properly, tend to make things a bit cleaner? I know these discussions of style can get heated -- no need to go there, just some practical suggestions please!

like image 794
rmw Avatar asked May 02 '11 22:05

rmw


2 Answers

As teresko says, MySQL names are case sensitive on *NIX platforms and insensitive on Windows. If you develop code to support both (as I do) then mixing your cases can cause huge headaches: for example, dump a database on Windows and restore it onto *NIX and all your cases are lost. We actually had to kludge code to detect and fix the cases in a dump just for this reason.

If you're free of Windows though it doesn't really matter what you use, as long as you keep it consistent.

like image 52
Oliver Emberton Avatar answered Nov 16 '22 01:11

Oliver Emberton


When it comes to models & database tables, you can use:

  • CamelCase for model names,
  • plural form of your model's name for database table (with consistent lower/uppercases, like eg. 'camelcases'),
  • table names in alphabetical order, separated by underscore (eg. 'camels_cases' being connection table between 'cases' and 'camels'),

For classes I would generally use CamelCases (beginning with upper case) and camelCases for methods (beginning with lower cases).

But, in fact, what counts is consistency and readability. It may be a good idea to follow naming conventions of some of the well known and widely implemented frameworks, such as Zend Framework (this one provides pretty precise guidelines as far as coding standard is concerned), but eg. Kohana may be also a good idea. Reinventing the wheel may not be the best idea ;)

like image 25
Tadeck Avatar answered Nov 16 '22 00:11

Tadeck