Intro To PSR (PHP standards recommendations)

Moath Obeidat
3 min readFeb 26, 2022

This article is a simple introduction to a very large topic, so this article will provide you with a quick introduction and resources, to give you the starting point to dive deeper into this interesting topic.

What is PSR?

PSR stands for PHP standards recommendations.

When did the need for these standards arise?

before PSR there were no truly uniform standards for writing PHP.
so there is a need to make common rules and standards to follow between PHP developers to make common visual style, naming conventions, and other technical settings allow us to produce a homogenous code that is easy to read and maintain.

Who wrote these standards?

A group of people, representing various popular PHP projects came together in 2009 and formed something called Framework Interoperability Group(FIG). The purpose of FIG is for project representatives to talk about the commonalities between their projects and find ways to work together.

And this group of people is voting for these standards, some standards have been deprecated after being accepted.

How many standards do we have?

13 accepted standards
2 deprecated standards
3 abounded standards
4 draft standards

so in total, we have 21 standards that have been written.

What are the main topics that these standards cover?

  • Autoloading
  • Interfaces
  • Http
  • Coding styles

PSR Rules

So after giving you a simple intro let’s take some standards and examples

PSR-1 and PSR-2 are taking care of PHP coding standards

let’s have an overview of PSR-1 Rules

  • You may know a three different ways to open a PHP tag but PSR-1 asked you to use only these two <?php ?> and <?= ?>.
  • Class names MUST be declared in StydlyCaps.
  • Method names MUST be declared in camelCase.
  • Namespaces and classes MUST follow PSR-0.
  • and there is more you can find here (PSR-1)

this rule said that “Namespaces and classes MUST follow PSR-0”, but did you know that PSR-0 was deprecated? even PSR-2 is deprecated!
but some PSR is still a prerequisite to other PSR even if they are deprecated, like PSR-0 in PSR-1.

some PSR has two prerequisites to follow like PSR-12 which requires PSR-1 and PSR-2 rules in addition to its own rules.

If you want to go deep into this topic and read all the PSR, take a look at the official website here PHP-FIG

Conclusion

every PSR have a lot of rules and the happy news, that you don’t have to remember all these PSR and its rules because modern PHP frameworks take care of this part and follow PSR, and following the framework that you are using rules would be enough, but you still have to take care about your code to follow naming conventions for classes and methods, and your code format but the format part can be solved easily using IDE extensions like Php-cs-fixer which behind the scenes follow PSR and can be configured with custom rules.

--

--