DEV Community

Anders Björkland
Anders Björkland

Posted on

Do what you like but frowns may be had, or PHP Standard Recommendations

Just the Gist

To give guidelines to PHP projects and to ease how they can interact with each other, a group of project maintainers formed the PHP Framework Interop Group. This group has since come to produce a set of PHP Standard Recommendations (PSR). Recommendations describe such things as code structure and autoloading. PSRs are not rules, they are recommendations.

Finding structure among the flexibility of PHP

With such an open community and flexibility as the PHP language has, there should be a lot of different ways to code with PHP. And there are! On December 2nd we say that this flexibility was part of why it has become so popular. Procedural, object oriented, and functional programming all have a place in PHP. So as not to become a free-for-all shouting match whenever a group of developers are cooperating, there are a set of recommendations that they can follow. (Of course, this doesn't mean there won't be something else to shout about!)

The set of recommendations available to the public community are called PHP Standard Recommendations (PSR). The most basic of these recommendations are PSR-1 (Basic Coding Standard), and with a total of 14 standards currently in effect there are many questions that can be addressed just from visiting the official PSR page. Here's the list of all the currently accepted recommendations:

  • PSR-1: Basic Coding Standard
  • PSR-3: Logger Interface
  • PSR-4: Autoloading Standard
  • PSR-6: Caching Interface
  • PSR-7: HTTP Message Interface
  • PSR-11: Container Interface
  • PSR-12: Extended Coding Style Guide
  • PSR-13: Hypermedia Links
  • PSR-14: Event Dispatcher
  • PSR-15: HTTP Handlers
  • PSR-16 Simple Cache
  • PSR-17: HTTP Factories
  • PSR-18: HTTP Client

Where the standards come from

Some standards has been abandoned and others has been deprecated as the PHP language and general coding practices has evolved. But we haven't asked who makes these decisions. The answer is: The PHP Framework Interop Group (PHP-FIG). It's a community of representatives from some of the most popular PHP projects, such as Composer, Drupal, Laminas Project, and TYPO3. Previous member projects included Laravel and Symfony. The aim of PHP-FIG is to find commonalities between different projects and how they can work together. PSRs are therefore not to be seen as a set of rules, and are de-facto often a kind of interface chosen on per project basis. For example: PSR-4 (Autoloading Standard) describes how namespaces of classes are used for autoloading their files, but this is not the one and only way anyone can or should implement autoloading.

What about you?

Have you heard of PSRs or PHP-FIG before? Do you think PSRs are a good addition to PHP? Do these recommendations come in the way of how you like to code PHP? Do you prefer to see them as a set of recommendations, or would you see more projects adhere to them as rules? Comment below and let us know what you think ✍

Further Reading

Top comments (0)