Davechild - PHP Fig PSR Standards
Davechild - PHP Fig PSR Standards
Official Site http://www.php-fig.org Files MUST use only <?php and <?= tags. Opening braces for control structures MUST go
Files MUST use only UTF-8 without BOM for on the same line, and closing braces MUST go
Accepted Standards on the next line after the body.
PHP code.
Files SHOULD either declare symbols (classes, Opening parentheses for control structures
PSR-0 Autoloading Standard
MUST NOT have a space after them, and
functions, constants, etc.) or cause side-
http://www.php-fig.org/psr/psr-0/ closing parentheses for control structures
effects (e.g. generate output, change .ini
PSR-1 Basic Coding Standard MUST NOT have a space before.
settings, etc.) but SHOULD NOT do both.
http://www.php-fig.org/psr/psr-1/
Namespaces and classes MUST follow PSR-0.
PSR-2 - General
PSR-2 Coding Style Guide Class names MUST be declared in
http://www.php-fig.org/psr/psr-2/ StudlyCaps. Code MUST follow all rules outlined in PSR-1.
PSR-3 Logger Interface Class constants MUST be declared in all upper All PHP files MUST use the Unix LF (linefeed)
PSR-2 - Namespace and Use Declarations PSR-2 - Classes, Properties, and Methods PSR-2 - Control Structures
(cont)
When present, there MUST be one blank line There MUST be one space after the control
after the namespace declaration. There MUST NOT be a space after the structure keyword
opening parenthesis of a method, and there
When present, all use declarations MUST go There MUST NOT be a space after the
MUST NOT be a space before the closing
after the namespace declaration. opening parenthesis
parenthesis.
There MUST be one use keyword per There MUST NOT be a space before the
declaration. In the argument list, there MUST NOT be a closing parenthesis
space before each comma, and there MUST
There MUST be one blank line after the use There MUST be one space between the
be one space after each comma.
block. closing parenthesis and the opening brace
Method arguments with default values MUST
The structure body MUST be indented once
go at the end of the argument list.
PSR-2 - Classes, Properties, and Methods
The closing brace MUST be on the next line
Argument lists MAY be split across multiple
The extends and implements keywords MUST after the body
lines, where each subsequent line is indented
be declared on the same line as the class once. When doing so, the first item in the list The body of each structure MUST be enclosed
name. MUST be on the next line, and there MUST be by braces.
The opening brace for the class MUST go on its only one argument per line. The keyword elseif SHOULD be used instead
own line; the closing brace for the class MUST When the argument list is split across multiple of else if.
go on the next line after the body. lines, the closing parenthesis and opening The case statement MUST be indented once
Lists of implements MAY be split across brace MUST be placed together on their own from switch, and the break keyword (or other
multiple lines, where each subsequent line is line with one space between them. terminating keyword) MUST be indented at the
indented once. When doing so, the first item in When present, the abstract and final same level as the case body.
the list MUST be on the next line, and there declarations MUST precede the visibility There MUST be a comment such as // no
MUST be only one interface per line. declaration. break when fall-through is intentional in a non-
Visibility MUST be declared on all properties. When present, the static declaration MUST empty case body.
The var keyword MUST NOT be used to come after the visibility declaration.
declare a property. PSR-2 - Closures
When making a method or function call, there
There MUST NOT be more than one property MUST NOT be a space between the method or Closures MUST be declared with a space after
declared per statement. function name and the opening parenthesis, the function keyword, and a space before and
there MUST NOT be a space after the opening
Property names SHOULD NOT be prefixed with after the use keyword.
parenthesis, and there MUST NOT be a space
a single underscore to indicate protected or The opening brace MUST go on the same line,
before the closing parenthesis.
private visibility. and the closing brace MUST go on the next line
Visibility MUST be declared on all methods. In the argument list, there MUST NOT be a following the body.
space before each comma, and there MUST
Method names SHOULD NOT be prefixed with There MUST NOT be a space after the
be one space after each comma.
a single underscore to indicate protected or opening parenthesis of the argument list or
private visibility. variable list, and there MUST NOT be a space
Method names MUST NOT be declared with a before the closing parenthesis of the argument
In the argument list and variable list, there Every method accepts a string as the Logging exceptions is a common pattern and
MUST NOT be a space before each comma, message, or an object with a __toString() this allows implementors to extract a stack
and there MUST be one space after each method. trace from the exception when the log backend
comma. supports it.
The message MAY contain placeholders which
Closure arguments with default values MUST implementors MAY replace with values from the Implementors MUST still verify that the
go at the end of the argument list. context array. 'exception' key is actually an Exception before
Argument lists and variable lists MAY be split Placeholder names MUST correspond to keys using it as such, as it MAY contain anything.
The LoggerInterface exposes eight methods Every method accepts an array as context contains a setLogger(LoggerInterface $logger)
matching log level names (see Log Levels data. This is meant to hold any extraneous method and can be used by frameworks to
block) information that does not fit well in a string. The auto-wire arbitrary instances with a logger.
Psr\Log\InvalidArgumentException if the
If an Exception object is passed in the context
implementation does not know about the level.
data, it MUST be in the 'exception' key.
Users SHOULD NOT use a custom level
without knowing for sure the current
implementation supports it.
PSR-4 Specification
The fully qualified class name MUST have a top-level namespace name,
also known as a "vendor namespace".
The fully qualified class name MAY have one or more sub-namespace
names.
The fully qualified class name MUST have a terminating class name.