DEV Community

Anders Björkland
Anders Björkland

Posted on

The number one recommendation - PSR-1

Just the Gist

PHP Standard Recommendation 1 (PSR-1) is Basic Coding Standards for PHP. It is a set of coding standards that gives us a recommended way to structure our code.

The basics

Yesterday we had a first glance at PHP Standard Recommendations (PSRs). While these are not rules, they may provide just the structure needed to make spaghetti code in to lasagna. Today we see what PSR-1 (Basic Coding Standard) gives us for recommendation.

First, we use either <?php or <?= in our files. This means that we should avoid using <?.

The tag <? should be avoided since it can cause issues when generating XML documents. If you want to use it though, the setting short_open_tag in php.ini controls this.

Next, if we declare a new class, functions or constants in a file, we should avoid it causing side effects external to the file. This means for instance that we should not be updating any ini-settings or echoing out anything and then declare a class.

When we are on about classes, these should follow an autoloading standard. The current standard is PSR-4, which recommends us to use namespaces which goes hand-in-hand with the file structure. A class HomeController with a namespace App\Controller should be placed in folders and a file such as App\Controller\HomeController.php. Having multiple classes in a file is not recommended.

Following PSR-1, we get a structure that would look something like this:

<?php 
// Path: .\src\App\Controller\HelloController.php

namespace App\Controller;

class HelloController
{
    const HELLO_WORLD = 'Hello World';

    public function helloWorld()
    {
        echo self::HELLO_WORLD;
    }
}

Enter fullscreen mode Exit fullscreen mode

What's not specified in PSR-1 is how we capitalize or underscore our variables (non-constants). All this PSR asks us is to be consistent with how we use it, be it $PascalCase, $camelCase or $snake_case.

Expanding

A closely related PSR is PSR-12 (Extended Coding Standard). PSR-12 is a recommendation that adds some additional rules to PSR-1. Among other things, it let's us know where to put our declare statements when present, to omit ?> when dealing with files containing only PHP code, and how to use the use statements. If you feel the need to find more structure for your projects, PSR-1 and PSR-12 are good places to start!

What about you?

Do you see any benefits of following Basic Coding Standard? Perhaps you already did, but didn't know it was PSR-1 or PSR-12 that you were following? Do you agree with PSR-1? Should PSR-1 also have given us a recommended way to write our variable-names? Comment below and let us know what you think ✍

Further Reading

Oldest comments (2)

Collapse
 
fchaussin profile image
François CHAUSSIN • Edited

Yep! The basics! without convention, reading code can give a big headache .
Don't really need to be psr1, but it exist, and now, i can't imagine not sticking to it.
When i read psr1 random code, i feel at home.

Collapse
 
andersbjorkland profile image
Anders Björkland

Yes! It's not wrong to follow any other standard, but when it's PSR-1 and 12, it's just like home.