0% found this document useful (0 votes)
19 views

Davechild - PHP Fig PSR Standards

This document provides a cheat sheet overview of PHP-FIG coding standards including PSR-0, PSR-1, PSR-2, and other standards. It outlines the key requirements for file structure, naming conventions, formatting and more for PHP code following these standards.

Uploaded by

hermanmaraden
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Davechild - PHP Fig PSR Standards

This document provides a cheat sheet overview of PHP-FIG coding standards including PSR-0, PSR-1, PSR-2, and other standards. It outlines the key requirements for file structure, naming conventions, formatting and more for PHP code following these standards.

Uploaded by

hermanmaraden
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

PHP-FIG PSR Standards Cheat Sheet

by Dave Child (DaveChild) via cheatography.com/1/cs/1743/

Useful Links PSR-1 - Overview PSR-2 - Overview (cont)

Official Site http:/​/ww​w.p​hp-​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 parent​heses for control structures
PSR-0 Autolo​ading Standard
MUST NOT have a space after them, and
functions, constants, etc.) or cause side-
http:/​/ww​w.p​hp-​fig.or​g/p​sr/​psr-0/ closing parent​heses for control structures
e​ffects (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:/​/ww​w.p​hp-​fig.or​g/p​sr/​psr-1/
Namespaces and classes MUST follow PSR-0.
PSR-2 - General
PSR-2 Coding Style Guide Class names MUST be declared in
http:/​/ww​w.p​hp-​fig.or​g/p​sr/​psr-2/ Studly​Caps. 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)

case with underscore separa​tors. line ending.


http:/​/ww​w.p​hp-​fig.or​g/p​sr/​psr-3/
Method names MUST be declared in All PHP files MUST end with a single blank
PSR-4 Improved Autolo​ading
camelCase. line.
http:/​/ww​w.p​hp-​fig.or​g/p​sr/​psr-4/
The closing ?> tag MUST be omitted from files
PSR-2 - Overview containing only PHP.
PSR-0 - Overview
There MUST NOT be a hard limit on line
Code MUST follow PSR-1.
A fully-​qua​lified namespace and class must length.
have the following structure \<V​endor Name>​\ Code MUST use 4 spaces for indenting, not
The soft limit on line length MUST be 120
(<N​ame​spa​ce>​\)*​<Class Name> tabs.
charac​ters; automated style checkers MUST
Each namespace must have a top-level There MUST NOT be a hard limit on line
warn but MUST NOT error at the soft limit.
length; the soft limit MUST be 120 charac​ters;
namespace ("Vendor Name").
lines SHOULD be 80 characters or less. Lines SHOULD NOT be longer than 80
Each namespace can have as many sub- charac​ters; lines longer than that SHOULD be
There MUST be one blank line after the
na​mes​paces as it wishes. split into multiple subsequent lines of no more
namespace declar​ation, and there MUST be
Each namespace separator is converted to a than 80 characters each.
one blank line after the block of use
DIRECT​ORY​_SE​PARATOR when loading from There MUST NOT be trailing whitespace at the
declar​ations.
the file system. end of non-blank lines.
Opening braces for classes MUST go on the
Each _ character in the CLASS NAME is Blank lines MAY be added to improve
next line, and closing braces MUST go on the
converted to a DIRECT​ORY​_SE​PAR​ATOR. readab​ility and to indicate related blocks of
next line after the body.
The _ character has no special meaning in the code.
Opening braces for methods MUST go on the
namespace.
next line, and closing braces MUST go on the There MUST NOT be more than one statement
The fully-​qua​lified namespace and class is per line.
next line after the body.
suffixed with .php when loading from the file
Visibility MUST be declared on all properties Code MUST use an indent of 4 spaces, and
system.
and methods; abstract and final MUST be MUST NOT use tabs for indenting.
Alphabetic characters in vendor names,
declared before the visibi​lity; static MUST be PHP keywords MUST be in lower case.
namesp​aces, and class names may be of any
declared after the visibi​lity.
The PHP constants true, false, and null MUST
combin​ation of lower case and upper case.
Control structure keywords MUST have one be in lower case.
space after them; method and function calls
MUST NOT.

By Dave Child (DaveChild) Published 21st February, 2014. Sponsored by CrosswordCheats.com


cheatography.com/davechild/ Last updated 12th May, 2016. Learn to solve cryptic crosswords!
www.getpostcookie.com Page 1 of 4. http://crosswordcheats.com
PHP-FIG PSR Standards Cheat Sheet
by Dave Child (DaveChild) via cheatography.com/1/cs/1743/

PSR-2 - Namespace and Use Declar​ations PSR-2 - Classes, Proper​ties, 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 declar​ation. There MUST NOT be a space after the structure keyword
opening parent​hesis of a method, and there
When present, all use declar​ations MUST go There MUST NOT be a space after the
MUST NOT be a space before the closing
after the namespace declar​ation. opening parent​hesis
parent​hesis.
There MUST be one use keyword per There MUST NOT be a space before the
declar​ation. In the argument list, there MUST NOT be a closing parent​hesis
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 parent​hesis 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, Proper​ties, 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 parent​hesis 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. termin​ating 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 declar​ations MUST precede the visibility There MUST be a comment such as // no
MUST be only one interface per line. declar​ation. break when fall-t​hrough is intent​ional in a non-
Visibility MUST be declared on all proper​ties. When present, the static declar​ation MUST empty case body.
The var keyword MUST NOT be used to come after the visibility declar​ation.
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 parent​hesis, 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.
parent​hesis, 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 parent​hesis.
private visibi​lity. 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 parent​hesis of the argument list or
private visibi​lity. variable list, and there MUST NOT be a space

Method names MUST NOT be declared with a before the closing parent​hesis of the argument

space after the method name. list or variable list.

The opening brace of a method MUST go on its


own line, and the closing brace MUST go on
the next line following the body.

By Dave Child (DaveChild) Published 21st February, 2014. Sponsored by CrosswordCheats.com


cheatography.com/davechild/ Last updated 12th May, 2016. Learn to solve cryptic crosswords!
www.getpostcookie.com Page 2 of 4. http://crosswordcheats.com
PHP-FIG PSR Standards Cheat Sheet
by Dave Child (DaveChild) via cheatography.com/1/cs/1743/

PSR-2 - Closures (cont) PSR-3 - Message PSR-3 - Context (cont)

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 __toSt​ring() this allows implem​entors 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 placeh​olders which
Closure arguments with default values MUST implem​entors MAY replace with values from the Implem​entors MUST still verify that the
go at the end of the argument list. context array. 'excep​tion' key is actually an Exception before

Argument lists and variable lists MAY be split Placeh​older names MUST correspond to keys using it as such, as it MAY contain anything.

across multiple lines, where each subsequent in the context array.


line is indented once. When doing so, the first PSR-3 - Helper Classes and Interfaces
Placeh​older names MUST be delimited with a
item in the list MUST be on the next line, and single opening brace { and a single closing The Psr\Lo​g\A​bst​rac​tLogger class lets you
there MUST be only one argument or variable brace }. There MUST NOT be any whitespace implement the Logger​Int​erface very easily by
per line. between the delimiters and the placeh​older extending it and implem​enting the generic log
When the ending list (whether or arguments or name. method. The other eight methods are
variables) is split across multiple lines, the Placeh​older names SHOULD be composed forwarding the message and context to it.
closing parent​hesis and opening brace MUST only of the characters A-Z, a-z, 0-9, underscore The Psr\Lo​g\L​ogg​erTrait only requires you to
be placed together on their own line with one _, and period .. The use of other characters is implement the generic log method. Note that
space between them. reserved for future modifi​cations of the since traits can not implement interf​aces, in this
placeh​olders specif​ica​tion. case you still have to implement
PSR-3 - Log Levels (RFC 5424) Logger​Int​erface.
Implem​entors MAY use placeh​olders to
debug error implement various escaping strategies and The Psr\Lo​g\N​ull​Logger is provided together
translate logs for display. Users SHOULD NOT with the interface. It MAY be used by users of
info critical
pre-escape placeh​older values since they can the interface to provide a fall-back "​black hole"
notice alert not know in which context the data will be implem​ent​ation if no logger is given to them.
warning emergency displayed. However condit​ional logging may be a better
approach if context data creation is expensive.
PSR-3 - Basics PSR-3 - Context The Psr\Lo​g\L​ogg​erA​war​eIn​terface only

The Logger​Int​erface exposes eight methods Every method accepts an array as context contains a setLog​ger​(Lo​gge​rIn​terface $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) inform​ation that does not fit well in a string. The auto-wire arbitrary instances with a logger.

array can contain anything. The Psr\Lo​g\L​ogg​erA​war​eTrait trait can be


A ninth method, log, accepts a log level as first
used to implement the equivalent interface
argument. Calling this method with one of the Implem​entors MUST ensure they treat context
easily in any class. It gives you access to
log level constants MUST have the same result data with as much lenience as possible.
$this-​>lo​gger.
as calling the level-​spe​cific method. A given value in the context MUST NOT throw
Calling the log method with a level not defined an exception nor raise any php error, warning The Psr\Lo​g\L​ogLevel class holds constants for

by this specif​ication MUST throw a or notice. the eight log levels.

Psr\Lo​g\I​nva​lid​Arg​ume​ntE​xce​ption if the
If an Exception object is passed in the context
implem​ent​ation does not know about the level.
data, it MUST be in the 'excep​tion' key.
Users SHOULD NOT use a custom level
without knowing for sure the current
implem​ent​ation supports it.

By Dave Child (DaveChild) Published 21st February, 2014. Sponsored by CrosswordCheats.com


cheatography.com/davechild/ Last updated 12th May, 2016. Learn to solve cryptic crosswords!
www.getpostcookie.com Page 3 of 4. http://crosswordcheats.com
PHP-FIG PSR Standards Cheat Sheet
by Dave Child (DaveChild) via cheatography.com/1/cs/1743/

PSR-4 Specif​ication

A fully qualified class name has the following form: \<N​ame​spa​ceN​ame​>


(​\<Su​bNa​mes​pac​eNa​mes​>)*​\<C​las​sNa​me>

The fully qualified class name MUST have a top-level namespace name,
also known as a "​vendor namesp​ace​".

The fully qualified class name MAY have one or more sub-na​mespace
names.

The fully qualified class name MUST have a termin​ating class name.

Unders​cores have no special meaning in any portion of the fully qualified


class name.

Alphabetic characters in the fully qualified class name MAY be any


combin​ation of lower case and upper case.

All class names MUST be referenced in a case-s​ens​itive fashion.

A contiguous series of one or more leading namespace and sub-


na​mespace names, not including the leading namespace separator, in
the fully qualified class name (a "​nam​espace prefix​") corres​ponds to at
least one "base direct​ory​".

The contiguous sub-na​mespace names after the "​nam​espace prefix​"


correspond to a subdir​ectory within a "base direct​ory​", in which the
namespace separators represent directory separa​tors. The
subdir​ectory name MUST match the case of the sub-na​mespace names.

The termin​ating class name corres​ponds to a file name ending in .php.


The file name MUST match the case of the termin​ating class name.

Autoloader implem​ent​ations MUST NOT throw except​ions, MUST NOT


raise errors of any level, and SHOULD NOT return a value.

By Dave Child (DaveChild) Published 21st February, 2014. Sponsored by CrosswordCheats.com


cheatography.com/davechild/ Last updated 12th May, 2016. Learn to solve cryptic crosswords!
www.getpostcookie.com Page 4 of 4. http://crosswordcheats.com

You might also like