SlideShare a Scribd company logo
1 of 27
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

Similar to Psr - php standards recommendations

2021.laravelconf.tw.slides2
2021.laravelconf.tw.slides22021.laravelconf.tw.slides2
2021.laravelconf.tw.slides2LiviaLiaoFontech
 
Php psr standard 2014 01-22
Php psr standard 2014 01-22Php psr standard 2014 01-22
Php psr standard 2014 01-22Võ Duy Tuấn
 
PHP Standards Recommendations - PHP-FIG
PHP Standards Recommendations - PHP-FIGPHP Standards Recommendations - PHP-FIG
PHP Standards Recommendations - PHP-FIGAkshay Khale
 
Let's PHP in a better way! - Coding Recommendations.
Let's PHP in a better way! - Coding Recommendations.Let's PHP in a better way! - Coding Recommendations.
Let's PHP in a better way! - Coding Recommendations.Leekas Shep
 
An intro to php standards recommendation (psr)
An intro to php standards recommendation (psr)An intro to php standards recommendation (psr)
An intro to php standards recommendation (psr)valuebound
 
Presentation
PresentationPresentation
Presentationbugway
 
PHP Syntax & Comments
PHP Syntax & CommentsPHP Syntax & Comments
PHP Syntax & CommentsSidrah Noor
 
Psr 2 coding style guide - Tidepool Labs
Psr 2   coding style guide - Tidepool LabsPsr 2   coding style guide - Tidepool Labs
Psr 2 coding style guide - Tidepool LabsHarutyun Abgaryan
 
Tutorial on-python-programming
Tutorial on-python-programmingTutorial on-python-programming
Tutorial on-python-programmingChetan Giridhar
 
Coding standards PSR-1 & PSR-2
Coding standards PSR-1 & PSR-2Coding standards PSR-1 & PSR-2
Coding standards PSR-1 & PSR-2Aram Baghdasaryan
 
20 PHP Static Analysis and Documentation Generators #burningkeyboards
20 PHP Static Analysis and Documentation Generators #burningkeyboards20 PHP Static Analysis and Documentation Generators #burningkeyboards
20 PHP Static Analysis and Documentation Generators #burningkeyboardsDenis Ristic
 
Best practices in enterprise applications
Best practices in enterprise applicationsBest practices in enterprise applications
Best practices in enterprise applicationsChandra Sekhar Saripaka
 
Enforcing API Design Rules for High Quality Code Generation
Enforcing API Design Rules for High Quality Code GenerationEnforcing API Design Rules for High Quality Code Generation
Enforcing API Design Rules for High Quality Code GenerationTim Burks
 
Functional Programming In Jdk8
Functional Programming In Jdk8 Functional Programming In Jdk8
Functional Programming In Jdk8 Bansilal Haudakari
 
Functional Programming in JavaScript & ESNext
Functional Programming in JavaScript & ESNextFunctional Programming in JavaScript & ESNext
Functional Programming in JavaScript & ESNextUnfold UI
 
Listen and look at your PHP code
Listen and look at your PHP codeListen and look at your PHP code
Listen and look at your PHP codeGabriele Santini
 

Similar to Psr - php standards recommendations (20)

2021.laravelconf.tw.slides2
2021.laravelconf.tw.slides22021.laravelconf.tw.slides2
2021.laravelconf.tw.slides2
 
Php psr standard 2014 01-22
Php psr standard 2014 01-22Php psr standard 2014 01-22
Php psr standard 2014 01-22
 
PHP Standards Recommendations - PHP-FIG
PHP Standards Recommendations - PHP-FIGPHP Standards Recommendations - PHP-FIG
PHP Standards Recommendations - PHP-FIG
 
Let's PHP in a better way! - Coding Recommendations.
Let's PHP in a better way! - Coding Recommendations.Let's PHP in a better way! - Coding Recommendations.
Let's PHP in a better way! - Coding Recommendations.
 
An intro to php standards recommendation (psr)
An intro to php standards recommendation (psr)An intro to php standards recommendation (psr)
An intro to php standards recommendation (psr)
 
Php
PhpPhp
Php
 
Presentation
PresentationPresentation
Presentation
 
PHP Syntax & Comments
PHP Syntax & CommentsPHP Syntax & Comments
PHP Syntax & Comments
 
Psr 2 coding style guide - Tidepool Labs
Psr 2   coding style guide - Tidepool LabsPsr 2   coding style guide - Tidepool Labs
Psr 2 coding style guide - Tidepool Labs
 
Python
PythonPython
Python
 
Tutorial on-python-programming
Tutorial on-python-programmingTutorial on-python-programming
Tutorial on-python-programming
 
Coding standards PSR-1 & PSR-2
Coding standards PSR-1 & PSR-2Coding standards PSR-1 & PSR-2
Coding standards PSR-1 & PSR-2
 
20 PHP Static Analysis and Documentation Generators #burningkeyboards
20 PHP Static Analysis and Documentation Generators #burningkeyboards20 PHP Static Analysis and Documentation Generators #burningkeyboards
20 PHP Static Analysis and Documentation Generators #burningkeyboards
 
Best practices in enterprise applications
Best practices in enterprise applicationsBest practices in enterprise applications
Best practices in enterprise applications
 
Cpcs302 1
Cpcs302  1Cpcs302  1
Cpcs302 1
 
Enforcing API Design Rules for High Quality Code Generation
Enforcing API Design Rules for High Quality Code GenerationEnforcing API Design Rules for High Quality Code Generation
Enforcing API Design Rules for High Quality Code Generation
 
Python Tutorial for Beginner
Python Tutorial for BeginnerPython Tutorial for Beginner
Python Tutorial for Beginner
 
Functional Programming In Jdk8
Functional Programming In Jdk8 Functional Programming In Jdk8
Functional Programming In Jdk8
 
Functional Programming in JavaScript & ESNext
Functional Programming in JavaScript & ESNextFunctional Programming in JavaScript & ESNext
Functional Programming in JavaScript & ESNext
 
Listen and look at your PHP code
Listen and look at your PHP codeListen and look at your PHP code
Listen and look at your PHP code
 

Recently uploaded

Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...srsj9000
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxbritheesh05
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidNikhilNagaraju
 
Introduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHIntroduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHC Sai Kiran
 
An introduction to Semiconductor and its types.pptx
An introduction to Semiconductor and its types.pptxAn introduction to Semiconductor and its types.pptx
An introduction to Semiconductor and its types.pptxPurva Nikam
 
computer application and construction management
computer application and construction managementcomputer application and construction management
computer application and construction managementMariconPadriquez1
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.eptoze12
 
An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...Chandu841456
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfAsst.prof M.Gokilavani
 
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEINFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEroselinkalist12
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...asadnawaz62
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxDeepakSakkari2
 
Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxKartikeyaDwivedi3
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort servicejennyeacort
 
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsyncWhy does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsyncssuser2ae721
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerAnamika Sarkar
 
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor CatchersTechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catcherssdickerson1
 
8251 universal synchronous asynchronous receiver transmitter
8251 universal synchronous asynchronous receiver transmitter8251 universal synchronous asynchronous receiver transmitter
8251 universal synchronous asynchronous receiver transmitterShivangiSharma879191
 
Churning of Butter, Factors affecting .
Churning of Butter, Factors affecting  .Churning of Butter, Factors affecting  .
Churning of Butter, Factors affecting .Satyam Kumar
 

Recently uploaded (20)

Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptx
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfid
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
Introduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHIntroduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECH
 
An introduction to Semiconductor and its types.pptx
An introduction to Semiconductor and its types.pptxAn introduction to Semiconductor and its types.pptx
An introduction to Semiconductor and its types.pptx
 
computer application and construction management
computer application and construction managementcomputer application and construction management
computer application and construction management
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.
 
An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
 
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEINFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptx
 
Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptx
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
 
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsyncWhy does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
 
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor CatchersTechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
 
8251 universal synchronous asynchronous receiver transmitter
8251 universal synchronous asynchronous receiver transmitter8251 universal synchronous asynchronous receiver transmitter
8251 universal synchronous asynchronous receiver transmitter
 
Churning of Butter, Factors affecting .
Churning of Butter, Factors affecting  .Churning of Butter, Factors affecting  .
Churning of Butter, Factors affecting .
 

Psr - php standards recommendations

  • 1.
  • 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
  • 8. http://www.php-fig.org 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