Please wait

Understanding PSR

Coding standards are vital in PHP development as they ensure that code is consistent, readable, and maintainable. This fosters teamwork and makes troubleshooting easier for everyone, including beginners. For this reason, PSR was introduced into the PHP community, which has seen wide adoption.

PSR (PHP Standard Recommendation) is like a set of rules for writing PHP code. Imagine it as grammar rules for writing an essay. By following these rules, everyone writes code in a similar way, making it easier to read and understand, just like following grammar rules makes reading a book easier. If you're working with others, using the same rules means everyone can easily understand and work with the code.

Framework Interoperability Group

There isn't an official group or standard available. However, the most popular standards come from a group called PHP FIG. The PHP FIG group stands for PHP Framework Interop Group. It's like a team of people from different projects who work together to create standards for PHP coding.

They're responsible for creating PSRs (PHP Standard Recommendations). The PHP FIG creates and maintains these PSRs, helping developers write code in a consistent and compatible way across different projects. By following the PSRs, developers can work together more easily and create code that's simpler for everyone to understand and use.

Available Standards

A complete list of standards can be found here: https://www.php-fig.org/psr/

PSR NumberDescription
PSR-1Basic Coding Standard: Sets the fundamental standards for writing PHP code, such as naming conventions and class declarations.
PSR-3Logger Interface: Defines a common interface for logging libraries, making it easier to switch between different logging implementations.
PSR-4Autoloading Standard: Specifies rules for automatic loading of classes, making code organization and inclusion more efficient.
PSR-6Caching Interface: Describes a standardized way to use caching, improving code reusability across different caching implementations.
PSR-7HTTP Message Interface: Defines a common way to handle HTTP messages, streamlining interactions between web servers and clients.
PSR-12Extended Coding Style Guide: An extension of PSR-1 and PSR-2, it provides more specific rules for code formatting and structure.
PSR-14Event Dispatcher: Describes a standardized way to implement event management, allowing different parts of a system to communicate and react to events.
PSR-15HTTP Handlers: Defines interfaces for handling server-side HTTP requests and responses, promoting consistency in middleware development.
PSR-16Simple Cache Interface: Outlines a simple, unified way to use caching, allowing for easier integration and migration between caching systems.
PSR-17HTTP Factories: Provides interfaces for creating HTTP objects, encouraging uniformity across different HTTP factory implementations.
PSR-18HTTP Client: Describes a standard way to send HTTP requests and handle responses, making it easier to use different HTTP client libraries interchangeably.

Which standard do I use?

Deciding which PSRs (PHP Standard Recommendations) to use depends on your project's needs, the team's preferences, and sometimes the requirements of other tools or frameworks you are using. The choice of which PSRs to use is flexible, and you should make decisions based on the specific needs and context of your project. Adhering to standards can greatly enhance code quality and collaboration, but it's also important to select the ones that are most relevant and beneficial for your particular situation.

Here are some helpful tips on deciding which standard to integrate into your project:

  1. You can pick and choose. You don't have to use all the PSRs. You can select the ones that are relevant to your project. Many PSRs address different aspects of development, so it's common to pick the ones that fit your particular needs.
  2. Consider industry best practices. Sometimes, following widely-accepted standards (like PSR-1 and PSR-2 for coding style) can make it easier for other developers to understand and contribute to your code.
  3. It's possible to create a mix of PSRs and even add some custom rules specific to your project or organization, as long as they don't conflict with each other.
  4. Your development team might have preferences or specific coding guidelines that align with certain PSRs.
  5. Different projects may require different standards. For example, if you're working on a project that heavily involves HTTP communication, you might find PSR-7 and PSR-18 particularly useful.
  6. If you're using a specific framework or library, they might follow certain PSRs, and adhering to those same standards can make integration smoother.

PHP CodeSniffer

If you decide to follow these standards, there are tools available for ensuring that your code follows the standards outlined in a PSR. One of the most popular tools is PHP CodeSniffer, which can be found here: https://github.com/squizlabs/PHP_CodeSniffer

PHP CodeSniffer is a tool that helps developers write code that adheres to specific coding standards, such as the PSRs. It's like a spell checker for your code, analyzing it for compliance with the chosen rules and offering feedback on inconsistencies.

PHP CodeSniffer can be configured to check your code against specific PSRs (e.g., PSR-1, PSR-2). This ensures that the code follows the agreed-upon guidelines. Instead of manually reviewing the code for adherence to standards, PHP CodeSniffer automates the process. It can be integrated into your development environment or continuous integration pipeline to perform checks as you write or commit the code.

Not only does it identify coding standard violations, but it also provides information about what's wrong and how to fix it. Some configurations even allow it to automatically correct certain issues.

While it comes with built-in support for popular standards like the PSRs, PHP CodeSniffer is also highly customizable. You can define your coding standards based on the needs of your project or team.

You can install CodeSniffer with Composer using the following command:

composer require "squizlabs/php_codesniffer=*"

Next, you can run CodeSniffer in the command line:

./vendor/bin/phpcs -h

You can apply specific standards with the standard flag followed by the filename like so:

./vendor/bin/phpcs -sw --standard=PSR1 file.php

Key Takeaways

  • PSRs provide a unified way of writing code, promoting consistency across different projects and teams.
  • You can choose which PSRs to follow based on your project's needs and requirements. You don't have to adopt all the standards.
  • Tools like PHP CodeSniffer can be used to automate adherence to PSRs, identifying and even fixing violations.
  • Many modern PHP frameworks and libraries align with PSRs, so following them can make integration smoother.
  • PSRs are widely recognized in the PHP community, and following them can align your code with industry best practices.

Comments

Please read this before commenting