PHP code convention
All programming languages have one or two code conventions that are followed by all programmers using the language. But not PHP.
PHP has a lot of conventions so no developer follows any of them and writes the code as he wants. Also PHP has a lot of frameworks and every one of them has its own code convention.
So the problem is: How to choose a code convention?
And there are 2 ways: Use an existing one or follow your own.
It's nice to follow your own but best to use existing.
My decision on how to choose one was to put on a list the main topics and try to explain for myself why I should use this or that. After having answers on these main questions I chose an existing code convention which matches my preferences and adds all the little stuff that there's no need to explain.
The important word here is "explain". Because no existing convention says "why".
I've made these conclusions after about 10 years of coding, the last 5 of which - almost every day. If you don't like them - you are free to make your own. :)
Tabs vs spaces
If you haven't gone through this discussion you should know that this is the greatest programming question ever.
I say you should use tabs and most importantly here's why:
- Customizable - When working in a team some of the people prefer 4 spaces indentation, others - 2. If using tabs for indentation everyone can set the tab representation as he wants. Also when you have to check the code you have written years ago and your style has changed - you can customize it easy.
- Tab is made for indentation and therefore should be used for indentation.
- Using tabs instead of spaces you reduce the file size by 15%. Not believing - try it.
Curly braces placement
Curly braces should always be put on a separate line all by themselves. Always.
The reason behind this is that PHP is character-rich language - you can use all types of braces, all symbols you can write with the shift key and so on and so on. So if you put the opening braces on the same line and then start the body on the next line sometimes it becomes hard to read and understand what is going on. By putting the opening braces on the next line alone the body's scope is perfectly clear.
And all these braces should be used this way because this is the same character and there's absolutely no sense in writing it differently.
TRUE, FALSE and NULL
Should be written uppercase. The difference between the constants that should be written uppercase and these three is minimal. Also these are all used to compare with variables ($something), constants (CHRISTMAS), numbers (105) that start with capital character.
Files line ending: LF.
?> Close tag: Always missing.
I came up to comparing several of the famous existing code conventions and the conventions that the most famous frameworks use. The ones I compared were:
Let's compare the three topics that I wrote about early. For the braces let's get the control statements braces.
Name: Tabs/Spaces - Same/Next - Lower/Upper
The correct answers should be: Tabs - Next - Upper
- Zend: 4 spaces - Same - Lower
- PEAR: 4 spaces - Same - Lower
- PSR: 4 spaces - Same - Lower
- Laravel: Tabs - Next - Lower
- Phalcon: 4 spaces - Same - Lower
- Symfony: 4 spaces - Same - Lower
- CodeIgniter: Tabs - Next - Upper
- Yii: Tabs - Next - Lower
- CakePHP: Tabs - Next - Lower
- Kohana: Tabs - Next - Upper
- FuelPHP: Tabs - Next - Lower
- WordPress: Tabs - Same - Lower
- Drupal: 2 spaces - Same - Upper
- Joomla: 4 spaces - Same - Lower
- PrestaShop: Tabs - Next - Lower
So what we see here is that the old ones follow the PSR while the new ones use tabs for indenting and put the braces on the next line.
So I have to choose CodeIgniter or Kohana. After looking through the full conventions of these two frameworks I chose CodeIgniter's.
Here's a link to it: http://ellislab.com/codeigniter/user-guide/general/styleguide.html
If you wonder what convention you should use - take a look at the code you've written through your career. If you understand everything fast - that's a good sign. If it is a lot of effort to you - that's bad.
So whatever you choose - make sure you won't swear when you have to read it several years later.