PHP Coding Standards

Who needs coding standards?

Standards are boring. They take away our individual freedom. They are uncool. And they make all the differencde when groups of programmers are trying to coordinate their work and achieve maximum productivity.

PHP's humble beginings as a simple tagging language for "Personal Home Pages" has given the PHP community other advantages, in particular the ability to quickly deliver solutions while other developers are still defining their specifications. However, as PHP applications increase in complexity, professional techniques also gain increased importance.

Programming teams may use a version control system (VCS) to track their code changes. If each programmer formats the code differently, the VCS is filled with meaningless format changes. To avoid this, teams soon develop standards.

Individual teams can quickly agree to their own informal standard, but there is another level to this issue. As important frameworks and libraries converge, the trend toward standardization could lead to a time when code written for one framework can be more or less automagically refactored for others.

Eliminating CMS silos and the resulting Tower of Babble could launch a new wave of innovation as good ideas from each community filter more quickly into others. Projects that choose not to follow such standards may be be left in the dust. "Proudly not invented here." becomes a winning mantra.

PHP Framework Interoperability Group

The PHP Framework Interoperability Group has come to the rescue with proposed standards (PSRs) that PHP developers can follow to make it easier for libraries and frameworks to work together. The idea behind the group is for project representatives to talk about the commonalities between projects and find ways to work together. Their main audience is each other, but they're very aware that the rest of the PHP community is watching. If others want to adopt what they're doing they are welcome to do so, but that is not the aim. Many industry-leading frameworks such as Symfony, Drupal and TYPO3 already follow a shared set of standards based on PSR-1 and PSR-2.

  • PSR-1 Proposal – Basic Coding Standard. This section of the standard comprises what should be considered the standard coding elements that are required to ensure a high level of technical interoperability between shared PHP code.
  • PSR-2 Proposal – Coding Style Guide

The group includes key members of the following projects: phpBB, PEAR, PEAR2, Doctrine, Doctrine2, et al., Composer, Packagist, Zend Framework, Zikula, PPI, PPI2, Propel, Propel 2, Joomla, Drupal,  Apache log4php, Solar Framework, Aura Project, TYPO3 Flow, TYPO3 Neos, CakePHP, CakePHP 2, SugarCRM, Laravel,  Amazon Web Services SDK,  SabreDAV, Symfony, Symfony2, phpDocumentor, eZ Publish,  PyroCMS, Jackalope, Assetic, Buzz, and Agavi.

PHP Coding Standards Fixer

The PHP Coding Standards Fixer, from SensioLabs, helps developers identify code that varies from these standards.