Have you heard of PSR ?

You know sometimes things can get super weird. At first you may be sad, angry, confused, and even happy. You always get used to it though. It is human nature. One thing that I thought was weird when I first started doing PHP was the inconsistency between things. What are those things you say? Well… let me tell you.

What is the most common thing you are expected to do when you install PHP for the first time? Navigate to localhost to your freshly created index.php file and put the phpinfo() function to see what is going on? Yeap… It is as common as the boring “Hello World” examples or “if a class is a blue print then a house is an object” bla bla bla. When you open that page for the first time and you see the output from your function, you are thinking “cool… everything is working” at this point. What I thought was “should this function name not be camel cased?”. Well, may be because it is an internal function. BUT, lets take a look at another common PHP function. Lets see … There is str_split() that comes into my mind. Wait a second! That one is underlined? Which is it then? How should I write my code? I read a study once and it concluded that people today in general are not as happy as the previous generation because today we have more choices. We end up wasting our time trying to decide rather than maximizing what is available. I honestly believe that because now I am confused…

Fear not though fellow coder. Some cool PHP people (they go by the name of PHP-FIG as in Framework Interop Group) finally agreed upon some things. They called those agreements PSR which stands for PHP standards recommendation. You can submit your application to join them if you like. Take notice of the “recommendation” part. If you choose not to follow the standards, PHP compiler can not even pretend to care and your app will work just fine. On the other hand, when you try to contribute to a project, let’s say on GitHub, your pull request may get rejected for not following the standards. Someone on StackOverflow most likely will point out that your style is wrong even if that was not your question at all. When you apply for a job and the HR person asks for coding samples, he or she might be okay with it but the nerd who is going to review your code pass the HR person may not be. You just lost your chance of a new job. So if you are starting to code PHP, learn these and just follow them. It is why I’ve selected this topic to be the first one for PHP. If you have been coding PHP for a while and your style does not match the recommendations (like me), stop, embrace the change (like me) and go with it like most of the world.

Enough messing about. Lets get on with the standards. Well, so far there are six of them.

  • PSR-1: Basic coding style
  • PSR-2: Strict code style (not to be confused with strict mode in JavaScript. Remember these are just recommendations)
  • PSR-3: Logger interface
  • PSR-4: Autoloading standard
  • PSR-6: Caching interface
  • PSR-7: HTTP Message interface

Before we get into details for each one of them in upcoming posts, I just want to say, just keep in mind to use the PSR-2 coding style standard. Trust me it will play out better for the long haul.

Posted in PHP