PHP Coding Standard
PHP Coding Standard
For Developers
Presented On 13-Feb-2014
By,
Sangeeta Arora – Technical Lead | CIPL
[email protected]
The fully-qualified namespace and class is suffixed with .php when loading from the file
system.
Alphabetic characters in vendor names, namespaces, and class names may be of any
combination of lower case and upper case.
Files MUST use only UTF-8 (without BOM) character encoding 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.
Class constants MUST be declared in all upper case with underscore separators, if required.
E.g. const VERSION; const DATE_APPROVED;
Files
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.
Lines
There MUST NOT be a hard limit on line length.
The soft limit MUST be 120 characters; lines SHOULD be 80 characters or less.
There MUST NOT be trailing whitespace at the end of non-blank lines.
Blank lines MAY be added to improve readability and to indicate related blocks of code.
There MUST NOT be more than one statement per line.
Lists of implements MAY be split across multiple lines, where each subsequent line is
indented once. When doing so, the first item in the list MUST be on the next line, and
there MUST be only one interface per line.
Opening braces for classes MUST go on the next line, and closing braces MUST go on the
next line after the body.
Properties
Visibility MUST be declared on all properties.
Methods
Visibility MUST be declared on all methods.
Method names SHOULD NOT be prefixed with a single underscore to indicate protected
or private visibility.
Method names MUST NOT be declared with a space after the method name.
The opening brace MUST go on its own line, and the closing brace MUST go on the next
line following the body.
13 This is private and confidential material
PSR – 2 – Coding Style Guide …contd.
Methods
There MUST NOT be a space after the opening parenthesis, and there MUST NOT be a
space before the closing parenthesis. e.g.
Method Arguments
There MUST NOT be a space before each comma.
There MUST be one space after each comma.
Method arguments with default values MUST go at the end of the argument list.
Example of Arguments in
the same line
Example of Arguments in
the same line Example of Arguments not in the same
line, when have a long list of arguments.
There MUST be one space between the closing parenthesis and the opening brace.
The closing brace MUST be on the next line after the body.
Note the placement of parentheses, spaces, and braces for do-while statement.
Note the placement of parentheses, spaces, and braces for foreach statement.
Long description is where the bulk of the documentation goes; it can be multi-line and as
long you wish.
28 This is private and confidential material
DocBlocks …contd.
Tags
The tags are used to specify additional information, such as the expected parameters and their type.
Each tag starts on a new line, followed by an at-sign (@) and a tag-name followed by white-space and
meta-data (including a description).
@author – The @author tag is used to document the author of any "Structural Element".
@copyright – The @copyright tag is used to document the copyright information of any
"Structural Element".
@example – The @example tag is used to link to an external source code file which contains
an example of use for the current "Structural element".
@license – The @license tag is used to indicate which license is applicable for the associated
'Structural Elements'.
@method – The @method allows a class to know which 'magic' methods are callable.
@property – The @property tag is used in the situation where a class contains the __get()
and __set() magic methods and allows for specific names.
@see – The @see tag can be used to define a reference to other "Structural Elements" or to
a URI.
@todo – The @todo tag is used to indicate whether any development activities should still
be executed on associated "Structural Elements".
@uses – The @uses tag describes whether any part of the associated "Structural Element"
uses, or consumes, another "Structural Element" or a file that is situated in the current
project.
It is an essential development tool that ensures your code remains clean and consistent.
It can also help prevent some common semantic errors made by developers.
It allows you to not only ensure that your code follows the PSR standards, but it also uses
PHP’s linter to check for syntax errors.
Multiple coding standards can be used within PHP_CodeSniffer so that the one installation
can be used across multiple projects.
The default coding standard used by PHP_CodeSniffer is the PEAR coding standard.