PHP Standards Recommendations - PSR

Sep 05, 2021 By Puneet verma

Whenever the use of technology rises, everyone comes up with their own way of using it. This results is in difficulties in using technology itself.

Example:
Everyone follows their own naming conventions and coding style, like Method names are written in  various styles like cameCase, snake_case etc

All these made it hard to work across different frameworks and in different versions of PHP.

So with an increase in PHP usages and new frameworks are coming. It was felt to have a standard set of rules.

Standardization in PHP is required to achieve

  • Interoperability of components
  • Produces a homogenous code that is easy to read and maintain.
  • Provide a common technical basis for implementing proven concepts for optimal programming and testing practices.

PHP Framework Interoperability Group(PHP-fig) is formed that includes several PHP frameworks founders.

This set of standardization name as PHP Standards Recommendations in short PSR, In Java, we have JSR

As for the PSR standards themselves, there are various standard but only 7 have been accepted by all members.


PSR-0 - Autoloading Standard
It describes the mandatory requirements that must be adhered to for autoloader interoperability.

This is not in use anymore - you should use PSR-4.

PSR-1 - Basic Coding Standard
It 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 - Coding Style Guide
PHP keywords (true, false, null) must always be in lower case
The extends and implements must be on the same line as the class name
etc

psr PHP standard recommendation

There are many more PSRs

Read more about PSR