Coding style PHP Best practices
Respect PSR-1 and PSR-2
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!
Note: Some IDEs like PHPStorm already have a CS-Fixer implemented.
Use an advanced rule set
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.
Found a typo? Something is wrong in this documentation? Just fork and edit it!