I’ve been working in a very small team for PHP development and we’ve been using an ad-hoc, “Invented Here” style guide. Now that our team and codebase is growing, we would like to use a more broadly-used style guide that would be, if not an official standard, a de facto standards used in many PHP communities. Does such a thing exist as it exists in the C/C++/Java/Javascript/etc. circles ?
The PHP Framework Interop Group has released a series of coding standard recommendations:
- PSR-0: Autoloading Standard
- PSR-1: Basic Coding Standard
- PSR-2: Coding Style Guide (this is what you are looking for)
- PSR-3: Logger Interface
FIG’s recommendations are far from an official standard, and I wouldn’t even consider them a de facto standard, but they are the more coherent set of recommendations for PHP we have.
Other than FIG’s recommendations, you should take a look at Zend Framework’s standards, which include coding style recommendations, and PEAR’s standards. Personally I tend to favour FIG’s recommendations, they are a bit more coherent than every other set of PHP standards I’ve seen and are more widely adopted.
You should also take a look at PHP CodeSniffer. It’s a tool that detects violations of a defined (and configurable) set of coding standards and can be easily automated via Phing. If you decide to follow FIG’s recommendations, you could also use PHP-CS-Fixer to rewrite your code to be PSR-1 and PSR-2 compliant (with a pre-commit hook, for example).
Lastly, keep in mind that the main reason for adopting a coding style standard is consistency, you’ll never find a set of standards that will make everyone in your team happy. It doesn’t really matter which set of standards you’ll pick, what matters is that you pick one and stick with it.
0
Yes, there are couple of standards used in the PHP world. Two of them deserve special recomendation.
The PEAR standard, which should be used by all PEAR packages:
http://pear.php.net/manual/nl/standards.php
and the PSR standard, which is supported by some big PHP groups like Symfony, PEAR(again), Drupal, Joomla etc.
https://github.com/php-fig/fig-standards
The official PHP Coding Standard can be found in the root of their project:
https://github.com/php/php-src/blob/master/CODING_STANDARDS
UPDATE: Because people are already starting to down vote my answer. Be sure to actually read the above file. It contains the coding standards for the C part of PHP and for userland code. Specifically lines 136 to 166 are of interest. Of course the rest is also interesting.
TL;DR Build your standard on top of the PHP standard and it looks exactly as the language (from a userland view) does.
2