Chris Tankersley

PSRs - Embrace Them or Ignore Them

Posted on 2012-09-07

A few years ago a group was formed call the 'PHP Standards Working Group.' The idea behind this was noble - try and get large players in the PHP community to work together to make it easier to share code between projects. At the time there were competing autoloaders and standards by many of the big PHP projects so this was an attempt to bring everyone closer together.

This group would discuss and make recommendations to the PHP Community that was a part of the group. Eventually the name was changed to the 'Framework Interoperability Group' to better reflect what they were doing. Members of the FIG are encouraged to make recommendations and follow the standards so that developers could create code that worked across multiple projects, but even then members don't have to follow the standards.

If you've ever written code and then wished it worked with Drupal, you'll understand why this is a good thing.

Not long after the group's creation they announced PSR-0, which was an autoloading standard. Earlier this year they announced two more standards, PSR-1 and PSR-2, which covered coding styles. This sparked a religious war of sorts in the community not only against the standards announced, but also the FIG group.

I feel that a lot of it is unfounded due to ignorance of both FIG and what their intentions are.

So what are the standards?

PSR-0

PSR-0 covers autoloading of code. The Class name must match a certain structure and be mapped to a location on the file system. The Namespace and _ character are converted to DIRECTORY_SEPARATOR to help with this. You end up with something like this:

// This maps to Vendor\Project\MyClass
namespace Vendor\Project;

class MyClass { ... }

// This maps to Vendor\OtherProject\OtherClass
class Vendor_OtherProject_OtherClass { ... }

With a standard autoloader I can use rest assured that my code with will with Zend Framework 2, symfony 2, Agavi, and other projects in the FIG group. Granted, some projects like Drupal are still getting up to speed on this, but for developers this means no longer figuring out the autoloader per framework.

PSR-1

PSR-1 is the Basic Coding Standard. This sets up some basics like always using <?php or <?= tags to open PHP code, using UTF-8, and how to name Classes, Constants, and Methods. This is meant to ensure 'technical interoperability between code' so that autoloaders, IDEs, and developers don't have to think about how to name things.

PSR-2

PSR-2 is the Coding Style Guide, and is the one that is debated this most. This is the one that tells developers to use 4 spaces instead of tabs, where the curly brace should be, and is incredibly specific on how the code should be laid out.

The idea behind this one is to reduce cognative friction when switching between projects. You no longer have to remember to switch your IDE settings between a symfony2 project and a Zend Framework project, and when people look at your code it will match code they, and others, have written.

Why should you embrace them?

Writing your code against the standards introduced by the FIG group will make it easier for other people to work with your code by virtue of it being consistent with what they expect. More people will use your code because they know that it will autoload correctly without any tricks, and will work with many different systems.

If you are starting your own project, you can easily say that the project follows PSR-0, 1, and 2 and people will know what you are talking about. You don't have to keep your own style guide written.

As a developer, if you submit code to a project that is part of FIG you already know how they want things laid out, especially with PSR-2. Code is more consistent on how it is checked in, and you can run software that checks to make sure that your code matches PSR-1 and PSR-2.

Why can you ignore it?

FIG is not the creating large-scale PHP standards for every single developer. The PSR standards are meant for their members, and anyone else who wants to follow them.

You can ignore the PSRs because, unless you are part of the Framework Interoperability Group, no one is making you. Even members of the FIG are not required to follow every single standard. None of these PSRs are being enforced by the PHP Engine and I doubt they ever will. Keep your tabs and Allman style bracing. Do you love Whitesmith style? Great!

No one is going to force you to follow the PSRs. Keep in mind that if you work on a project that does adopt the FIG standards that you cannot commit code that goes against the PSRs. If you work on your own projects, feel free to continue doing what you do. No one with authority is going to e-mail you and tell you to change to PSR or your code won't work.


Comments