6. PSR
PSR (PHP Standards Recommendation)
php-fig.com (PHP Framework Interop Group)
Zend 2, Symfony 2, Yii 2, AWS SDK, CakePHP, Laravel
40+ members (www.php-fig.org/members/)
PHP-FIG mission:
Finding the way of working together
http://www.php-fig.org
7. NOTICE
Guide meaning?
Follow the rule STRICTLY
UNDERSTAND keywords
MUST, MUST NOT, REQUIRED, SHALL, SHALL NOT, SHOULD,
SHOULD NOT, RECOMMENDED, MAY, and OPTIONAL
(http://tools.ietf.org/html/rfc2119)
Clear the FIG & PSR PURPOSE
http://www.php-fig.org/faq/
Trung tâm lập trình Codeto - 2015
10. LOGGER INTERFACES
Libraries to receive a PsrLogLoggerInterface object and
write logs to it in a simple and universal way
The LoggerInterface exposes eight methods to write logs
(debug, info, notice, warning, error, critical, alert, emergency).
A ninth method log(), accepts a log level as first argument.
This method MUST have the same result as calling the level-
specific method
Trung tâm lập trình Codeto - 2015
11. PSR-1 Basic Coding Standard
High-level of PHP code
Files (PHP Tags, Character Encoding, Side Effects)
Namespace & Class Names (5.3 and later)
Class Constants, Properties, and Methods (Naming
Convention)
http://www.php-fig.org/psr/psr-1/Trung tâm lập trình Codeto - 2015
13. Style Guide
Files, Lines
Indent, tabs and spaces
Hard and soft limit
Open/Close braces“{“ and “}”
Classes, Properties and Methods
PSR-2 Coding Style Guide
Trung tâm lập trình Codeto - 2015
14. PSR-2 Overview
MUST
4 spaces indents and tabs
Soft limit 120 characters (should 80)
1 blank line after “namespace” and “use”
“{“ and “}” be next line for class and method
“{“ be the same line for control structure
public/protected/private (visibility) MUST be declared for
properties/methods
abstract/final/static before visibility
1 space after control structure keyword
Trung tâm lập trình Codeto - 2015
16. PSR-2 General
Files
MUST be end with single blank line and use Unix LF line ending
If PHP only, don’t use closing ?>
Lines
MUST NOT hard limit, soft limit must be 120
SHOULD be 80 and must not white space at the end
MUST not more than one statement/1 line
MAY be blank line for readable
Indents
MUST NOT use tabs for indents
MUST 4 spaces for indent
Keyword and True/False/Null
PHP keywords MUST be lower case
Constants true, false, null MUST be lower case
Namespace and Use
MUST be one blank line after “namespace” and “use” declaration
“use” MUST be after “namespace” declaration
Trung tâm lập trình Codeto - 2015
17. Classes
– Extends and Implements
• “extends” and “implements” keywords MUST on the same line
• Lists of implements MAY be split multiple lines
PSR-2 Classes, Properties, and Methods
Trung tâm lập trình Codeto - 2015
18. Properties
Visibility MUST be declared on ALL properties
MUST be ONE property PER statement
SHOULD be underscore (_) prefix for PROTECTED/PRIVATE
PSR-2 Classes, Properties, and Methods
Trung tâm lập trình Codeto - 2015
19. Methods
Visibility MUST be declared on all methods.
SHOULD NOT prefix underscore for protected/private
MUST NOT space after the method name.
“{“ MUST go on same line,
“}” MUST go on the next line the body.
MUST NOT space after the “(“ and before “)”
PSR-2 Classes, Properties, and Methods
Trung tâm lập trình Codeto - 2015
21. Method Arguments
MUST NOT space before comma
MUST be one space after comma.
Arguments with default values MUST be the end of
argument list.
MAY be split multiple lines then MUST one line per
argument
PSR-2 Classes, Properties, and Methods
Trung tâm lập trình Codeto - 2015
23. abstract, final, and static
abstract and static MUST before visibility
static MUST after visibility
PSR-2 Classes, Properties, and Methods
Trung tâm lập trình Codeto - 2015
24. MUST be one space after control structure keyword
MUST NOT be a space after “(“
MUST NOT be a space before “)”
MUST be one space between the “)” and “{“
Structure body MUST be indented once
The closing brace MUST be on the next line after the
body
PSR-2 Control Structure
Trung tâm lập trình Codeto - 2015
27. Tools
PHP Coding Standards Fixer
Check and Fix code follow PSR-1 and PSR-2
Customizable fixer (rules)
Command line
Configuration file
PHP version >= 5.3.6
http://cs.sensiolabs.org/
Trung tâm lập trình Codeto - 2015