Correct ways to build a house, to manufacture screens of certain resolutions or fitted sizes for the clothes we wear, correct ways of measurement - these are all standards that are everywhere around us. In the case of PHP, the standards are represented by the PSR specifications. We want to invite standards into the world of PHP, so that we can better understand the ecosystem around the PHP language and its future evolution.
Every programming language has standards and ways to solve problems that are considered best-practice in their world. Even PHP had something similar, but these standards were scattered in various corners of its community, with no recognizable entity that would centralize, govern and promote them.
In 2009 all this changed, because 5 PHP frameworks developers met at the php|tek 2009 conference in Chicago and decided to create a governing entity that would solve the problem of PHP standards once and for all. The group called itself PHP-FIG (PHP Framework Interoperability Group). Shortly after its birth, their first standard, called PSR-0, was published. This standard came as a result of the support for namespaces added by the newly launched 5.3 version of PHP; it standardized the way this new feature was used.
PSR stands for PHP Standard Recommendation. The way these standards work is very similarly to JSRs (Java Specification Request) from the Java world. They're numbered starting with 0 and contain a formal specification for a certain programming technique or a certain feature that can be implemented in PHP.
At the moment this article was written, there were 7 PSR standards, already published and used:
PSR-0. As mentioned in the previous chapter, this standard describes a correct and consistent way in which namespaces should be declared and used. PSR-0 is marked as deprecated in favour of the newer PSR-4.
PSR-1 specifies a set of general coding guidelines. They're usually more rudimentary and involve big-picture aspects of the code and files in which it is stored. There are great benefits when respecting this standard, since it significantly lowers the chances of incompatibilities between classes and libraries.
PSR-2 builds on PSR-1 and is one of the most substantial standards, compared to the other 6, because it contains a very extensive list of coding rules, mostly involving code style. This standard covers everything from methods, control structures and parentheses, to line length, indentation and rules about how many blank lines to leave between code structures.
PSR-3 defines a standard interface for libraries that implement the logging functionality. It also contains a lot of guidelines and advice about how such a library should be implemented and used.
PSR-4 is the successor of the older PSR-0 standard and it defines the correct way to declare and load namespaces from file paths. This standard is much more flexible compared to PSR-0 as far as syntax and auto-loading rules goes.
PSR-6 defines a set of interfaces and exceptions that should be implemented and used by libraries that provide caching functionality. Given the high detail and great scope in which everything is described, implementing this standard is significantly more difficult.
Some of the standards described above are not written from scratch, but instead are based on a few popular RFC references. Also, all PSR standards use the specifications of RFC-2119, which describes a common vocabulary to be used in formal technical specifications.
The working mechanisms of PHP-FIG are very similar to the ones used by the Java world for its JSR standards:
The group is made up of members that represent a specific framework or library
They propose a new standard, if there is sufficient interest from the community for that particular feature
New proposals are discussed and revised publicly, to bring them to a level of quality that is ready for voting
The proposals become published standards through the democratic vote of the group's members
Over time, there have been multiple controversies regarding the group's objective nature and its role in the PHP world. While some concerns might be sound, we need to keep in mind an important aspect regarding this group and the standards it proposes: implementing them or using them is completely optional.
This implies that PSR standards share a lot of properties with design-patterns:
They are considered to be the correct way to solve a problem
They were refined and polished by an army of intelligent programmers
They offer a common and compact vocabulary for common solutions
On top of all this, we have to remember the group's main goal: collaboration between frameworks (hence the group's name). Therefore, the standards they propose will tend to be related to features already present in most frameworks. The main idea and philosophy is to have an easy way to swap common libraries on which these frameworks depend.
Even though PHP-FIG is a controversial group that was founded a little too late, it still managed to impose itself as the absolute authority of PHP standards. The publishing of PSR standards by this group marked the beginning of a sustained maturity of the PHP ecosystem.
by Ovidiu Mățan
by Mihai Câsu
by Adrian Șuteu