Basics Every PHP Developer should know...
This is a real life example, you could find a PHP developer that never heard of PSR. The aim of this article is to highlight:
- What is PSR?
- The benefits of following these recommendations?
- Does it worth to just read these recommendations?
So Let's start with...
PHP Standards Recommendations (PSR)
Basically, It's a set of recommendations by PHP Framework Interop Group. These recommendations are a set of definitions on how to define a class, functions, namespace, logger, HTTP messages, and much more...
Did you ever ask yourself why many of the most known PHP frameworks declare core functions:
// PSR Recommendation
public function functionDeclaration()
{
}
// Not like this
public function functionDeclaration() {
}
Of course, it depends on your taste, but if you work within a team or would like to write a code that could be easily readable by other developers, then I would recommend to follow PSR-2 recommendations.
Benefits of following PSR
- Confident that your code could be easily readable by other PHP Devs
- It's always a good idea to have some coding standards within a team, so what's the point of reinventing the wheel
- At any time, you decide to publish your code/package, then it's easy for others to use it in their application. For example, could be a logger package, if it follows PSR-3, then it's way easier to be integrated in any project or simply replace an old package with this new one as long as both follow PSR-3 rules
- Easy to replace a package by another one if both follow PSR recommendations. For example, could use an HTTP Message Package and decide to replace it after few months by another package, as long as it follows PSR-7, then you are sure that your application should work as expected.
After reading the above lines...
Of course, this was a very short introduction of PSR, it's just to give an idea about it. Now, it's time to decide if it worth to read more and more about it (I really hope you do ;) ), Also could find more and more details here PSR