Psr - php standards recommendations

hason61vn 430 views 27 slides Mar 02, 2016
Slide 1
Slide 1 of 27
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27

About This Presentation

PSR là gì ?
Các chuẩn trong PSR


Slide Content

PHP Standards Recommendations

Topics SPMC Case-Study PSR Auto-loading Logger Interface PSR1 – Basic Coding Standards PSR2 – Coding Style Guides Tools Trung tâm lập trình Codeto - 2015

Case Study(1) Case-Sensitive PSEmployee or PsEmployee ? Windows fine - Unix die Raise Call to Undefined Class Errors Trung tâm lập trình Codeto - 2015

Case Study(2) Text Encoding WTF are displaying? Have you ever care? Raise incompatible character encodings errors Trung tâm lập trình Codeto - 2015

Case Study(3)

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

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

http://www.php-fig.org Trung tâm lập trình Codeto - 2015

AUTOLOADING Standard form: \< NamespaceName >(\< SubNamespaceNames >)*\< ClassName >

LOGGER INTERFACES Libraries to receive a Psr \Log\ LoggerInterface 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

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

Examples Trung tâm lập trình Codeto - 2015

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

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

Trung tâm lập trình Codeto - 2015

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

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

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

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

PSR-2 Classes, Properties, and Methods Trung tâm lập trình Codeto - 2015

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

Trung tâm lập trình Codeto - 2015

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

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

Trung tâm lập trình Codeto - 2015

Trung tâm lập trình Codeto - 2015

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
Tags