PSR-1 and PSR-2 to be Approved as Standards

Share this article

I hope you all know about the PHP Framework Interoperability Group
. The group proposes standards (PSRs) that developers can adhere to in order to make it easier to use their different libraries and frameworks together. The first proposal was PSR-0 for autoloading class definitions and was a huge success. Recently, the group found it important to address coding standards that should be used in different projects. This proposal was first proposed by Klaus Silveira and modified heavily by Paul M Jones after being discussed heavily done in the group’s mailing list. Of course there were a lot of differences in opinion and the group members fought in a friendly manner to bring about the PSR-1 and PSR-2 proposals. They initially started out as one proposal but the initial round of voting didn’t yield a majority in favor. Participants did however see merit in various requirements the decision was made to split it into 2 proposals — one for mandatory interoperability and one for suggested style. The voting on PSR-1 and PSR-2 has begun, and they will only be accepted as standards if they get a majority of votes. There are 20 members, and both PSR-1 and PSR-2 have over 11 votes meaning they will soon be accepted as standards. The proposals are: I appreciate Jones for his time and dedication as a leader to make the PSR. This will help to bring a wonderful future for the PHP community as a whole. There is more good news in that PSR-1 and PSR-2 can be enforced with PHP-CS-Fixer
. The goal is to automate the fixing of most issues, and the tool knows how to fix issues for the coding standards defined in the PSR-1 and PSR-2 documents. Thank you Fabien Potencier for your efforts on the sniffer; you did a great job. Image via Fotolia

Frequently Asked Questions about PSR-1 and PSR-2 Standards

What is the main purpose of PSR-1 and PSR-2 standards?

The primary purpose of PSR-1 and PSR-2 standards is to establish a set of coding standards for PHP. These standards aim to ensure a high level of technical interoperability between shared PHP code. By adhering to these standards, developers can ensure their code is clean, consistent, and easy to read, which is particularly important when working on large projects or collaborating with other developers.

How do PSR-1 and PSR-2 standards differ?

PSR-1, also known as the Basic Coding Standard, provides guidelines for elements that should be standardized in any PHP codebase. It covers topics like class and method naming, constants, and side effects. On the other hand, PSR-2, the Coding Style Guide, builds upon PSR-1 and provides more specific rules about how your code should look, including indentation, line length, and whitespace usage.

Why should I use PSR-1 and PSR-2 standards?

Using PSR-1 and PSR-2 standards can significantly improve the readability and maintainability of your code. It makes it easier for other developers to understand your code, reducing the time needed to familiarize themselves with it. This is particularly beneficial in open-source projects or large teams where code is frequently shared.

Are PSR-1 and PSR-2 standards universally accepted?

While PSR-1 and PSR-2 are widely accepted and used in the PHP community, they are not universally accepted. Some developers or teams may prefer to use their own coding standards. However, the PSR standards are recommended, especially for open-source projects, as they promote consistency and interoperability.

How can I check if my code adheres to PSR-1 and PSR-2 standards?

There are several tools available that can help you check if your code adheres to PSR-1 and PSR-2 standards. One of the most popular is PHP_CodeSniffer, which can detect violations of a defined set of coding standards.

Can I use PSR-1 and PSR-2 standards in older PHP versions?

PSR-1 and PSR-2 standards are designed to be compatible with PHP 5.3 and above. However, some of the standards may not be applicable or may need to be adapted for older PHP versions.

What happens if I don’t follow PSR-1 and PSR-2 standards?

Not following PSR-1 and PSR-2 standards won’t cause your code to break, but it can make it harder for others to read and understand your code. It can also lead to inconsistencies in your codebase, making it more difficult to maintain and debug.

Are there any other PSR standards I should be aware of?

Yes, there are several other PSR standards that provide guidelines for different aspects of PHP programming. These include PSR-3 for logging interfaces, PSR-4 for autoloading standards, and PSR-7 for HTTP message interfaces, among others.

How often are PSR standards updated?

PSR standards are not frequently updated, but new standards are occasionally proposed and approved by the PHP-FIG. It’s a good idea to regularly check the PHP-FIG website or subscribe to their mailing list to stay updated on any changes.

Where can I learn more about PSR-1 and PSR-2 standards?

The official PHP-FIG website is the best place to learn more about PSR-1 and PSR-2 standards. It provides detailed explanations of each standard, along with examples and best practices. Other resources include PHP community forums and blogs, as well as online coding platforms that offer tutorials on PHP standards.

Hari K THari K T
View Author

Hari K T is a Freelance LAMP developer/consultant, open-source contributor auraphp and speaker.

Share this article
Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week
Loading form