Learning about PHP Coding Style

Jangwook Kim
Jan 26 · 3 min read

I know that common coding style is helpful for cooperation. But unfortunately, I’m working alone so I didn’t understand it’s importance.

When I decide refactoring my legacy source code, I found the QUITE DIRTY CODE. So I make up my mind studying about PHP coding style.

PSR-1: Basic Coding Standard

PHP Basic coding standard are described in RFC 2119.

  1. PHP code MUST use only<?php &<?= tags.
  2. PHP code MUST use only UTF-8 without BOM.
  3. A file SHOULD either declare new symbols (classes, functions, constants, etc.) or cause side effects, but SHOULD NOT do both.
  4. Namespaces and classes MUST follow PSR-0: Autoloading Standard & PSR-4: Autoloader.
  5. Classes MUST be declared in Pascal Case(PascalCase) or
  6. Constants of classes MUST be declared in All Caps Snake Case(ALL_CAPS_SNAKE_CASE).
  7. Methods MUST be declared in Camel Case(camelCase).

PSR-12: Extended Coding Style

It’s the expansion of PSR-2, so it replaces PSR-2. Extended coding style are also described in RFC 2119.

So many rules are defined in PSR-12, I just write most common (or things that I will use).


  1. The closing tag(?>) MUST be omitted if files contain only PHP.


  1. There MUST NOT be trailing white space at the end of lines.
  2. For improve readability, blank lines MAY be added except where explicitly forbidden.
  3. There MUST NOT be more then one statement per line.


  1. Indent MUST NOT be tabs for indenting.

Keywords and Types

  1. Short form of type keywords MUST be used.
    ※ Use boolinstead of boolean.


  1. The comment or statement MUST NOT be allowed after the closing brace.
  2. When instantiating new class, parentheses MUST always be present.
  3. The extends and implements MUST be located on the same line as the class name.

Regards of Classes

  1. When using traits, one blank line MUST be displayed after the use statements.
  2. Visibility MUST be declared on all properties and constants, methods if PHP supports.
  3. The underscore MUST NOT be used to indicate private or protected visibility.
  4. In the argument list, there MUST NOT be spaces before comma, MUST be one space after comma.
  5. When declare return type, there MUST be one space after the colon. There MUST NOT be space between the question mark and type if return type is nullable.
  6. The abstract and final keyword MUST be located before the visibility declaration.
  7. The static declaration MUST be located after the visibility.

Parentheses of the Control Structures

  1. There MUST NOT be any space within ther parentheses.


Surely, there are too many rules, and it’s hard to remember. So we can fix our code using PHP CS(code sniffer).

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade