SlideShare a Scribd company logo
Psr - php standards recommendations
Topics
1. SPMC Case-Study
2. PSR
 Auto-loading
 Logger Interface
 PSR1 – Basic Coding Standards
 PSR2 – Coding Style Guides
3. 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 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
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

More Related Content

DOCX
PDF
Test Presentation
PPTX
Java generics(Under The Hood Of The Compiler) by Harmeet singh
PPT
Chap04
PPTX
Interfaces and abstract classes
PDF
8 abstract classes and interfaces
PPT
Php Presentation
PDF
PHP CODING STANDARDS
Test Presentation
Java generics(Under The Hood Of The Compiler) by Harmeet singh
Chap04
Interfaces and abstract classes
8 abstract classes and interfaces
Php Presentation
PHP CODING STANDARDS

Similar to Psr - php standards recommendations (20)

PDF
2021.laravelconf.tw.slides2
PPTX
Php psr standard 2014 01-22
PDF
PHP Standards Recommendations - PHP-FIG
PPTX
Let's PHP in a better way! - Coding Recommendations.
PPTX
An intro to php standards recommendation (psr)
PPT
PDF
Presentation
PPTX
PHP Syntax & Comments
PDF
Psr 2 coding style guide - Tidepool Labs
PPTX
Python
PDF
Tutorial on-python-programming
PDF
Coding standards PSR-1 & PSR-2
PDF
20 PHP Static Analysis and Documentation Generators #burningkeyboards
PPT
Best practices in enterprise applications
PPT
Cpcs302 1
PPTX
OCA_1Z0-808_Module00_Introduction_Java.pptx
PDF
Enforcing API Design Rules for High Quality Code Generation
PPTX
Python Tutorial for Beginner
PDF
ch02-Java Fundamentals-Java Fundamentals.pdf
PPTX
Functional Programming In Jdk8
2021.laravelconf.tw.slides2
Php psr standard 2014 01-22
PHP Standards Recommendations - PHP-FIG
Let's PHP in a better way! - Coding Recommendations.
An intro to php standards recommendation (psr)
Presentation
PHP Syntax & Comments
Psr 2 coding style guide - Tidepool Labs
Python
Tutorial on-python-programming
Coding standards PSR-1 & PSR-2
20 PHP Static Analysis and Documentation Generators #burningkeyboards
Best practices in enterprise applications
Cpcs302 1
OCA_1Z0-808_Module00_Introduction_Java.pptx
Enforcing API Design Rules for High Quality Code Generation
Python Tutorial for Beginner
ch02-Java Fundamentals-Java Fundamentals.pdf
Functional Programming In Jdk8
Ad

Recently uploaded (20)

PPT
Introduction, IoT Design Methodology, Case Study on IoT System for Weather Mo...
PDF
null (2) bgfbg bfgb bfgb fbfg bfbgf b.pdf
PPTX
Fundamentals of Mechanical Engineering.pptx
PDF
Categorization of Factors Affecting Classification Algorithms Selection
PPTX
additive manufacturing of ss316l using mig welding
PPTX
Current and future trends in Computer Vision.pptx
PDF
Well-logging-methods_new................
PPT
Total quality management ppt for engineering students
PPTX
Fundamentals of safety and accident prevention -final (1).pptx
PPTX
Construction Project Organization Group 2.pptx
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PPTX
Safety Seminar civil to be ensured for safe working.
PDF
R24 SURVEYING LAB MANUAL for civil enggi
PPT
Mechanical Engineering MATERIALS Selection
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PDF
737-MAX_SRG.pdf student reference guides
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PDF
Unit I ESSENTIAL OF DIGITAL MARKETING.pdf
Introduction, IoT Design Methodology, Case Study on IoT System for Weather Mo...
null (2) bgfbg bfgb bfgb fbfg bfbgf b.pdf
Fundamentals of Mechanical Engineering.pptx
Categorization of Factors Affecting Classification Algorithms Selection
additive manufacturing of ss316l using mig welding
Current and future trends in Computer Vision.pptx
Well-logging-methods_new................
Total quality management ppt for engineering students
Fundamentals of safety and accident prevention -final (1).pptx
Construction Project Organization Group 2.pptx
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
Foundation to blockchain - A guide to Blockchain Tech
Safety Seminar civil to be ensured for safe working.
R24 SURVEYING LAB MANUAL for civil enggi
Mechanical Engineering MATERIALS Selection
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
737-MAX_SRG.pdf student reference guides
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
CYBER-CRIMES AND SECURITY A guide to understanding
Unit I ESSENTIAL OF DIGITAL MARKETING.pdf
Ad

Psr - php standards recommendations

  • 2. Topics 1. SPMC Case-Study 2. PSR  Auto-loading  Logger Interface  PSR1 – Basic Coding Standards  PSR2 – Coding Style Guides 3. Tools Trung tâm lập trình Codeto - 2015
  • 3. 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
  • 4. 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
  • 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
  • 12. Examples 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
  • 15. 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
  • 20. 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
  • 22. 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
  • 25. Trung tâm lập trình Codeto - 2015
  • 26. 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