Servage Magazine

Information about YOUR hosting company – where we give you a clear picture of what we think and do!

PSR coding-standards for PHP

Wednesday, December 28th, 2016 by Servage

phpWhen PHP 1.0 was released in 1995, there were no common standards or code style preference for the language.  This continued to be the case until 2009 when PHP PSR (PHP standard recommendation), a set of standards was finally introduced to the language. The recommendations consist of multiple standards, so let’s see what the major ones are.

PSR-1: Basic Coding Standard

The basic coding standard defines a set of rules that all PHP files should follow. The purpose of it is to make shared PHP code as compatible as possible with other projects and environments where the code is executed.

For example, you should only use the full <?php or short-hand <?= tags in PHP files. The <? tag should not be used because it requires that the short_open_tag option is enabled in php.ini. Because this is not the case on all servers running PHP, you should aim to use the two tags that are always enabled.

Another thing worth mentioning in the PSR-1 is that class names should be written in PascalCase and method names should be in camelCase. According to PSR-1, all classes should also follow an autoloading standard, which we will cover later in this article.

PSR-2: Coding Style Guide

The purpose of the second PSR standard is to make reading other developers’ code easier. If all developers write their code in a similar fashion and pay attention to these things, reading the code of other developers gets easier and faster because you do not have to first familiarize yourself with the coding style the file was written in. Instead, because the code looks familiar, you can jump directly into inspecting what the code does.

One commonly argued practise in PHP and other languages is the position of the opening curly bracket of methods. Some prefer to keep it on the same line and others place it on a new line. If you follow the PSR standards, you should place the opening brace on a new line from now on. The same applies to classes.

This may not sound like a big deal, but you should be using spaces instead of tabs. The practise of mixing tabs and spaces in code can confuse “diff tools” such as the tool you can use in Git to compare changes between commits.

PSR-4: Autoloading Standard

The fourth standard can be a little tricky for those who are not familiar with Composer or autoloading. To summarize, it means that instead of using a separate include statement for each class you want to use in your PHP file, you can load a single autoloader file. If the files you want to autoload use a common standard, the autoloader file will know where to look for the files you are looking for.

The autoloading standard is what defines what the directory structure of third-party libraries and other code should look like to make them available for autoloading. If you are curious about how this works in more detail, have a look at this and the other standards at

PSR coding-standards for PHP, 4.0 out of 5 based on 4 ratings
Categories: Guides & Tutorials, Tips & Tricks


You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

No comments yet (leave a comment)

You are welcome to initiate a conversation about this blog entry.

Leave a comment

You must be logged in to post a comment.