I believe you already heard about the PSR in PHP language. Today I am trying to help you to understand the what is PSR, how and why should we use it.
PSR in PHP
PSR knock as ‘PHP Standards Recommendation’ which is some recommendations for all PHP programmer when they write their code they should follow some rules and regulation. With following that recommendations anyone can understand other programmers code. It’s working like an eco-system or uniform system where we all following these systems. Basically, there are five PSR: PSR-0, PSR-1, PSR-2, PSR-3, PSR-4. So let’s see about these five.
Before we move forward we should need to know the history of PSR in PHP language. Honestly, there are no uniform coding standards for writing code for PHP programmer. Some programmer has written their code by with their own naming convention and coding style. Someone was following some framework’s conventions such as PEAR or Zend Framework. So there was a huge problem to understand other programmer’s code. Cause they do not follow any uniform rules and regulation.
With that passing time, FIG (Framework Interoperability Group) intends to introduce a cross-section of the PHP ecosystem, not exclusively framework developers. The main focus of FIG create a dialogue between project representatives, and with this concept, they were searching a suitable and useful way to work together and everyone comes under one ecosystem of PHP and they introduce some PHP Standards Recommendations (PSR-0, PSR-1, PSR-2, PSR-3, PSR-4).
PSR-0 refers to the ‘Autoloader Standard’. it comes with huge advantages to reusable the codes and loading the classes. Before the PSR-0 has come, we were working with PHP 5 magic function _autoload() which is automatically loading the class we required and gives us a flexible way to create the instance that class. But with _autoload() you can able to provide only one autoloader function. Later PHP 5.12 introduced spl_autoload() which provides to register multiple autoloader function. Then PHP introduces the spl_autoload_register() function which gives us permission to create a series of the autoloading function. After PHP 5.3 PHP support true namespace and then the FIG (Framework Interoperability Group) written some guideline for the PSR-0. Below are the requirements for PSR-0 compliance:
- A fully-qualified namespace and the classes must have the following conventions <Vendor Name>(<Namespace>)*<ClassName>.
- Each and Every namespace must have a top-level namespace (“Vendor Name”).
- A namespace can have as many as sub-namespaces according to need.
- Namespace separator converted into DIRECTORY_SEPARATOR.
- Underscore in the class name converted into DIRECTORY_SEPARATOR.
- Vendor names, namespaces, and class names might be of any combination of lowercase and uppercase.
These are some requirements for the PSR-0.
PSR-1 refers to ‘Basic Coding Standard’ for PHP programmers. Actually, PSR-1 focus on the naming convention, file structure etc. With these rules, the code is understandable to all programmers who follow the PSR-1 Rules. Below are the requirements for PSR-0.
- Only allow to write <?php and ?> (<? and <? are not allowed) tags for PHP.
- Only allow using UTF-8
- Force to use PSR-0
- Class name must be in PascalCase/StudlyCaps (that means follow Capitalization Conventions while write class name )
- Class constants/global variable must be defined in UPPER_CASE with underscore separators.
- Method names must be defined in camelCase.
These are some requirements for the PSR-1.
PSR-2 refers to ‘Coding Style Guide’ . Actually PSR-2is the extended and expands of PSR-1’s ‘Basic Coding Standard’. The main goal of the this Standard is moving all PHP programmer to follow a uniform coding guideline
- The programmer must follow a coding style guideline (PSR-1).
- Code must have 4 space for indenting, tabs are not allowed.
- Code should have the length limit (120 characters are known as soft limit and above 120 characters knock as hard limit. The code length limit should be 80 characters or less)
- There MUST be one empty line after the namespace declaration, and there MUST be one blank line after the block of use declarations
- Opening braces for classes MUST go on the next line, and closing braces MUST go on the next line after the body.
- Opening braces for methods MUST go on the next line, and closing braces MUST go on the next line after the body.
- Visibility MUST be declared on all properties and methods; abstract and final MUST be declared before the visibility; static MUST be declared after the visibility.
- Control structure keywords MUST have one space after them; method and function calls MUST NOT.
- Opening braces for control structures MUST go on the same line, and closing braces MUST go on the next line after the body.
- Opening parentheses for control structures MUST NOT have space after them, and closing parentheses for control structures MUST NOT have space before.
These are some requirements for the PSR-2.
PSR-3 refers to ‘Shared Logger Interface’ for PHP programmers. Which is for taking/storing the application/system log reports. When an application is running then sometimes error, notice, alert, can be happened, in this situation we need to track/save these actions, and PSR-3 describe us how should we save these actions.
PSR-3 incorporating with eight Syslog levels of(debug, info, notice, warning, error, critical, alert and emergency), These Syslog levels describe as a function and it’s accept two parameters such as ‘Message’ and ‘Context’. Generally ‘Message’ is a string regarding with logs level and Context is for store additional information.
Recently monolog implemented PSR-3 so you can use monolog for storing your application log.
PSR-4 is the most recent added by FIG (Framework Interoperability Group). The PSR-4 is working together with PSR-0 where needed. PSR-4 is the replacement of PSR-0. The main goal of PSR-4 is to remove the remnants of PSR-0 and the pre-5.3 days completely and allow for a more concise folder structure. You can see the specifications of PSR-4 from here
The PHP-FIG does a great job of centralizing PHP standards. With this following these standards every PHP programmers will going forward with the same concept. So every PHP programmer should follow these standards, so they can working together.