Coding style PHP Best practices

Respect PSR-1 and PSR-2



PHP now has a coding style guide shared by many projects. Its name is PSR-2. You should follow it.


At TheCodingMachine, most of us hate spaces. We prefer tabs. So why not ask for developers to use tabs? Because PSR-2 says we should use spaces. We might not like everything in PSR-2 but at least, it is a shared standard. It means that if you follow it, your code will follow the same standard as the vast majority of projects out there.

Oh... one very important thing! Do not spend any time learning PSR-2. This is useless! Instead, use a code formatting tool that will do the work for you!

Use PHP-CS-Fixer. or PHP Code Sniffer. Both tools will do all the work for you. Run PHP-CS-Fixer or Code Sniffer before any commit you plan to do.

Note: Some IDEs like PHPStorm already have a CS-Fixer implemented.

Use an advanced rule set



While PSR-2 is considered easy to follow, there are more advanced and stricter rule set you can use. You will require more work to implement but lead to high quality code.


PHP Code Sniffer comes with a number of extensions that add additional rules.

Here is a list:

  • Doctrine coding standard: the coding standard used by the Doctrine project. It is very strict. In particular, it forces typing everything (so this is great).
  • Hard mode: a slightly modified version of the Doctrine coding standard that we enjoy using
  • Slevomat coding standard: a set of rules you can cherry-pick to build your own coding standard (if you feel like it). The Doctrine coding standard and "hard mode" are based on those rules.
If you are starting a new project, by all means, please use an advanced rule set.
However, if you are already working on an existing project with a large code base, this might require a large effort for a small to medium gain.

Found a typo? Something is wrong in this documentation? Just fork and edit it!

image description