1. Home
  2. >
  3. What is PSR: PHP coding standards

What is PSR: PHP coding standards

Reading time6 min.
Article created
Updated

PSR (PHP Standards Recommendation) is a set of PHP-community recommendations for the development of a standardized and harmonized code in the PHP programming language. PSR is designed to facilitate the development and support of PHP-projects and to improve the readability and understanding of the code by different developers.

Coding standards defined in the PSR help developers work together on projects and improve the readability of the code, which in turn facilitates its support and expansion. The PSR has recommendations on various aspects of coding, ranging from class and methods to file and dependency management.

Responsible developers should adhere to PSR standards in order to create a readable and harmonized code, which would subsequently greatly assist other developers in their work on the project.

The ultimate objective of the PSR is to create agreements that can use most of the PHP projects in order to ensure uniformity and easy service of PHP projects. Compliance with these standards helps to create the most readable and supported code, which is an integral part of the development of the PHP reference code.

Principles of standard PSR

  • Simple and understandable. The PSR standards document simple rules and indicate us the basic principles to be followed in the PHP code.
  • Coherence. PSR standards offer generally accepted principles that should be applied when writing the code in PHP projects. This helps to create a uniform and harmonized coding style that allows for better reading and maintaining the code in the future.
  • Enlargement. PSR standards offer a core set of rules and recommendations that can be complemented and expanded according to the specific requirements of the project or team of developers.
  • Compatibility. PSR standards are compatible with other PHP standards, such as PSR-0, PSR-1, PSR-2, PSR-3, PSR-4 and others.
  • Result. PSR standards improve the productivity of development, facilitate the escort and support of the code and improve its quality.
  • Popularity. Many popular PHP-projects and Freimvours (e.g. Symfony, Laravel, Zend Framework) follow PSR standards, making these standards widespread and recognized in the community of developers.

Use of PSR standards

Coding standards in PHP, known as PSR (PHP Standard of Recommendations), are a set of rules and recommendations that help developers write a quality and compatible code. Monitoring PSR standards has several significant benefits.

  1. Improved code reading

    The PSR standards are followed by uniform style and code formatting, which facilitates its reading and understanding for all project participants. The code written in accordance with the PSR becomes more understandable and accessible to other developers.

  2. Facilitation of joint work

    PSR standards provide a single set of rules for all developers. This greatly facilitates joint work on the project, especially when the team has different levels of experience and preferences for coding style.

  3. Improved code quality

    Monitoring PSR standards helps developers avoid common errors and practice better code writing practices. This can improve the quality and reliability of the application, as well as facilitate the support and finalization of the code in the future.

  4. Accelerate the development process

    PSR standards offer classification agreements for classes, interfaces, methods and variables, which help to reduce the time spent on discussion and decision-making on the code structure. Coordinated names facilitate the search for and understanding of functionality in large projects.

In general, compliance with PSR standards allows for a more qualitative, readable and compatible code in PHP projects. The monitoring of these rules helps to simplify the joint work of the developers and improve the quality of the software being produced.

Existing PSR standards in PHP

In PHP, developers actively use standards to improve the quality of the code, its readability and support. One of the most popular PHP coding standards is PSR (PHP Standards Recommendations).

There are currently several versions of PSR standards, each of which has its specification and is intended for certain development tasks.

1. PSR-1 Basic Coding Standard (Standard of Basic Coding)

PSR-1 defines the basic coding rules to be applied in PHP projects. The standard defines rules for class names, interfaces, methods and properties, and standard quality requirements for the code.

2. PSR-2 Coding Style Guide

PSR-2 expands PSR-1 standards by establishing additional rules for the formatting of the code. The standard defines the rules of derogation, equation, use of empty lines, comment of the code and other important nuances of the PHP code.

3. PSR-3 Logger Interface (Logger interface)

PSR-3 contains an interface that should be used for the labelling of PHP applications. This standard allows for standardizing the work with the loggers and ensuring compatibility between the different libraries.

4. PSR-4 Autoloader

PSR-4 defines the standards for class loading in PHP projects. This standard allows for a convenient automatic switching of classes without the need to use multiple requirements-include and supports the correct structure and title of classes.

5. PSR-6 Cache Interface (Cash interface)

PSR-6 contains interfaces and classifications for data cashing in PHP appes. Using this standard, developers will be able to create compatible and transportable e-components for their projects.

This is not a complete list of PSR standards in PHP, but they are the most known and widely used in the developer community. Each standard provides guidance on the design of the code and structure of the project, thus making your code more readable, supported and compatible with other PHP libraries and projects.

Process of development and adoption of PSR standards

PHP coding standards known as PSR (PHP Standards) are developed and implemented jointly by the PHP community to improve the quality and consistency of the code.

The development of each PSR standard consists of the following steps:

  1. Idea: Any member of the PHP community may propose an idea for a new PSR standard. To this end, a issue or pull request can be created in an official repository php-fig on GitHub.
  2. Design: The idea is discussed by the community, including PHP FIG (Framework Interoperability Group), which decides to write a specific standard. A working group composed of participants from the community is then established to develop the text of the standard.
  3. Discussion: The text of the standard developed is discussed in the community and may include testing of the standards by both PHP FIG organizers and other communities.
  4. Implementation: Once the text of the standard reaches the agreement of the majority of participants, it may be adopted as a PSR. At this stage, the working group is responsible for introducing and monitoring the standard adopted in the official repositories.

The adoption of PSR standards is done by voting PHP FIG. It is important to note that PSR standards are not mandatory, but are aimed at establishing agreed rules for the broad PHP community.

The process of developing and adopting PSR standards was open and the active participation of each PHP member was welcomed. This enables uniformity in the development of PHP applications and promotes language development.

Question-record

What is PSR?

PSR is a reduction from PHP Standards Recommendation. This set of guidelines and recommendations developed by the PHP-community for uniform coding and project management in PHP.

What problems does PSR solve?

PSR solves a number of problems related to the uncoding style and project organization in PHP. In particular, this helps to improve the readability and support of the code, enables the developers to focus more quickly on the project, improves teamwork and facilitates the integration of the outside libraries.

What is the structure of the PSR?

PSR consists of several recommendations, each of which describes the specific programming aspect. For example, PSR-1 and PSR-2 relate to the coding style, PSR-3 defines the standards for logging and PSR-4 describes the class loading in the project.

What does PSR standards need to be observed for?

Compliance with PSR standards helps to create a code that will be understood by other developers and facilitates the support and escort of the project. In addition, compliance with PSR standards allows the use of side libraries that also adhere to these standards.

How to use PSR standards in your draft?

To use the PSR standards, the draft should read the relevant recommendations (e.g. PSR-1 and PSR-2) and follow the guidance provided therein. It is also recommended that instruments be used to automatically verify compliance with the code with PSR standards, such as CodeSniffer or IDE standards.