Pages

Monday, August 13, 2012

PHP Coding standards

For more than a year, some influent PHP programmers of the most active of the most active projects in the community have been working on coding standards.

The group is name PHP Framework Interoperability Group and is composed of, but not limited to, authors from these projects:
  • phpBB
  • PEAR
  • Doctrine
  • Composer / Packagist
  • Joomla
  • Drupal
  • CakePHP
  • Amazon Web Services SDK
  • Symfony
  • Zend Framework

Why coding standards?

You may be a fan, for example of naming your functions with underscore or use tab indentation, but really this is not point. The goal is to be able to use code from other authors and projects without having to "fix" the code style to be consistent with your project.

In the future, the group also aims at providing some interfaces so implementations from different can work together.

Accepted standards

  • PSR-0 Code structure, Class and function naming
  • PSR-1 Basic coding standards
  • PSR-2 Coding style standards (mostly whitespace)
See full repository: https://github.com/php-fig/fig-standards

I don’t want to rewrite all my code!

Well, you probably don’t need to.

Chances are that you are already pretty near PSR-0 if you have organized your classes to be autoloaded. If you haven’t, you should really look forward to it, autoloading eliminate the need to require classes, simplifying a lot class dependencies.

After complying to PSR-0, there’s a tool that will do almost all the hard work for you by fixing all the whitespace. It is called PHP-CS-Fixer and is from Fabien Potencier, member of the group.

You can try PHP_CodeSniffer, but personally a find it a pain to use because it only 'validates' and make some errors. It should probably need a rewrite.

Some editors have plugins:
You can also add a Git commit hook to patch on-the-fly.

Further reading

Great blog post from Paul M. Jones.

No comments:

Post a Comment