Frogg logo

Frogg's web tools

Hand Crafted Tools

Home > Coding PHP > PHP Standards Recommendations (PSR)
Welcome on Frogg's web tools | Current date :
21/11/2024

PHP Standards Recommendations (PSR)

Official doc : PHP Standards Recommendations

According to the PSR Workflow Bylaw each PSR has a status as it is being worked on. Once a proposal has passed the Entrance Vote it will be listed here as “Draft”. Unless a PSR is marked as “Accepted” it is subject to change. Draft can change drastically, but Review will only have minor changes.

As also described in the PSR Workflow Bylaw. The Editor, or editors, of a proposal are the essentially the lead contributors and writers of the PSRs and they are supported by two voting members. Those voting members are the Coordinator who is responsible for managing the review stage and votes; and a second sponsor.
  • PSR evolves, so they must be checked frequently.

Accepted PSR (18/12/2017)

PSR Coding Overview

PSR-1: Basic Coding Standard

  • Files MUST use only <?php and <?= tags.
  • Files MUST use only UTF-8 without BOM for PHP code.
  • Files SHOULD either declare symbols (classes, functions, constants, etc.)
    or cause side-effects (e.g. generate output, change .ini settings, etc.)
    but SHOULD NOT do both.
  • Namespaces and classes MUST follow an “autoloading” PSR: [PSR-0, PSR-4].
  • Class names MUST be declared in StudlyCaps.
  • Class constants MUST be declared in all UPPER CASE with underscore separators.
  • Method names MUST be declared in camelCase.

PSR-2: Coding Style Guide

  • Code MUST follow a “coding style guide” PSR [PSR-1].
  • All PHP files MUST use the Unix LF (linefeed) line ending.
    All PHP files MUST end with a single blank line.
    The closing ?> tag MUST be omitted from files containing only PHP.
  • Code MUST use 4 spaces for indenting, not tabs.
  • There MUST NOT be a hard limit on line length; the soft limit MUST be 120 characters;
    lines SHOULD be 80 characters or less.
  • PHP keywords MUST be in lower case.
    The PHP constants true, false, and null MUST be in lower case.
  • There MUST be one blank line after the namespace declaration, and there
    MUST be one blank line after the block of use declarations.
  • Opening braces for classes MUST go on the next line, and closing braces MUST
    go on the next line after the body.
  • Opening braces for methods MUST go on the next line, and closing braces MUST
    go on the next line after the body.
  • The extends and implements keywords MUST be declared on the same line as the class name.
  • Visibility MUST be declared on all properties and methods; abstract and
    final MUST be declared before the visibility; static MUST be declared
    after the visibility.
    The var keyword MUST NOT be used to declare a property.
    There MUST NOT be more than one property declared per statement.
    Property & Method names SHOULD NOT be prefixed with a single underscore to indicate protected or private visibility.
  • In the argument list, there MUST NOT be a space before each comma, and there MUST be one space after each comma.
    Method arguments with default values MUST go at the end of the argument list.
  • Control structure keywords MUST have one space after them; method and
    function calls MUST NOT.
  • Opening braces for control structures MUST go on the same line, and closing
    braces MUST go on the next line after the body.
  • Opening parentheses for control structures MUST NOT have a space after them,
    and closing parentheses for control structures MUST NOT have a space before.
PSR2 Coding Standards visualization (from code sniffer)
Page created by the 18/02/2018 18:36
Generated in 0.001 sec & displayed in ... sec