How to write better PHP code
Simple answer: use automated code inspectors with ruleset (coding standard) that is official for framework you currently use.
Installing Code Sniffer for PHP
sudo pear install PHP_CodeSniffer
DO NOT use version from other package managers because its seriously outdated at the moment and won’t work with most of IDE’s
Personally I use PHP Storm, if you use other IDE unfortunately you’re on your own
File -> Default Settings -> PHP -> Code Sniffer
Enable in project
This step has to be repeated in each project, however if you for example develop only Symfony 2 applications you can set it to enabled by default in Default Settings, from this moment each project created will have this validation enabled by default.
File -> Settings -> Inspections -> PHP -> PHP Code Sniffer validation
In Coding standard select ruleset you want to use
Ruleset for PSR-2
Ruleset for Symfony 2
https://github.com/opensky/Symfony2-coding-standard If you encounter any problem with this how to please leave a comment so I can improve it. If this post helped you let me know, it’s always nice to know that I could help someone :)