SlideShare a Scribd company logo
PHP Standards
Recommendations
(PSR)
Akshay Khale

(Sr. Software Engineer)
– Martin Fowler
“Any fool can write code that a computer can
understand. Good programmers write code that
humans can understand.”
PSR-1: Basic Coding
Standards.
• MUST use only <?php and <?= tags.

• MUST use only UTF-8 Encoding.

• SHOULD either declare symbols or cause side-effects.

• Class names MUST be declared in CamelCase or
StudlyCaps.

• constants MUST be declared in all UPPER_SNAKE_CASE.

• Method names MUST be declared in camelCase.
Quiz Time: What is wrong with
following code snippets?
PSR-2: Coding Style Guide
• 4 Space indenting (No Tabs).

• Soft Line limit 120 Characters.

• Blank space after namespace declaration before use.

• Closing Brace “}” always on a separate line.

• Opening Brace “{” Same line (with a space before) for control statements
and next line for Classes and functions.

• Space before open parentheses “(” for control statements and not for
functions.

• Visibility mode is must for all properties and methods. abstract and final
before visibility and static after the visibility.
Quiz Time: What is wrong with
following code snippets?
PSR-3: Logger Interface
• Only allowed Log levels: debug, info, notice, warning, error,
critical, alert, emergency.

• MUST implement PsrLogLoggerInterface.

• MUST throw PsrLogInvalidArgumentException exception
on invalid log level.

• public function log($level, $message, array $context =
array());
• One can consider extending PsrLogAbstractLogger class
which implements LoggerInterface.
Sample Logger Class:
https://github.com/akshaykhale1992/luxio-logger
PSR-4: Autoloader
• Namespace: <vendor_namespace>/
<package_namespace>[/<subnamespace>/]/
<class_name>.

• Folder structure should also follow the namespace
pattern.
PSR-6: Caching Interface
• MUST implement PsrCacheCacheItemPoolInterface and
PsrCacheCacheItemInterface.

• Concepts: Pool, Items, Key, Data, TTL, Hit, Miss.

• Should not throw any Exception.
PSR-7: HTTP message
interfaces
• Request Structure

• Response Structure

• Interfaces: PsrHttpMessageRequestInterface and
PsrHttpMessageResponseInterface.
PSR-11: Container interface
• MUST implement PsrContainerContainerInterface.

• MUST throw PsrContainerNotFoundExceptionInterface
exception if object not found.

• Directly thrown exceptions must implement
PsrContainerContainerExceptionInterface interface.
Standards that most of the
developers follow.
• If a file defines a Class then it should not have any other declarations though PSR
allows.

• Files with functions should have only function definitions.

• Files with class / functions definitions should not have a closing PHP Tag (?>) and
should have an empty line before EOF.

• Helper files must have function_exists check.

• Code MUST be correctly formatted based on the PSR rules. (editor plugins can be used
for the same).

• Arrays properties should be on separate lines.

• All declarations MUST have dock-blocks.

• Branch names MUST follow bitbucket Branching Model.
Cont.
• Use static instead of self for calling static class members.

• Use die instead of exit.

• Make use of destructors for unsetting objects and/or closing opened files.

• There should not be any commented code or commented die / dd statements.

• SHOULD avoid else block in case of if statements.

• Recommended to make use of exceptions in-case of invalid details which should be
handles at calling end.

• Try to use User defined constants instead of System constants. A constants class should
be created for defining all the constants that are being used in the project. E.g. instead of
using “/” for directory paths use DIRECTORY_SEPARATOR or instead of using supplier_id
== 7 check use supplier_id == Constants::DELHIVERY_SUPPLIER_ID.

• PHP Unit Test cases are compulsory.
Samples:
Thank you!!!

More Related Content

PDF
Coding standards PSR-1 & PSR-2
PPTX
Gul Ahmed Textile Mills Ltd.- Survey.
PPT
Gul ahmed
PDF
Unleashing the Power of Generative AI.pdf
PPTX
ChatGPT for Data Science Projects
PPTX
An intro to php standards recommendation (psr)
PPTX
PSR: Standards in PHP by Alex Simanovich
PPTX
The World of PHP PSR Standards
Coding standards PSR-1 & PSR-2
Gul Ahmed Textile Mills Ltd.- Survey.
Gul ahmed
Unleashing the Power of Generative AI.pdf
ChatGPT for Data Science Projects
An intro to php standards recommendation (psr)
PSR: Standards in PHP by Alex Simanovich
The World of PHP PSR Standards

Similar to PHP Standards Recommendations - PHP-FIG (20)

PDF
Current status of PSR - Phpblt1
PPTX
Php psr standard 2014 01-22
PDF
Meet up symfony 16 juin 2017 - Les PSR
PDF
Submit PHP: Standards in PHP world. Михайло Морозов
PDF
PHP CODING STANDARDS
PDF
Psr 2 coding style guide - Tidepool Labs
PPTX
Let's PHP in a better way! - Coding Recommendations.
PDF
2021.laravelconf.tw.slides2
PDF
Making the Most of Modern PHP in Drupal 7
PPTX
Programming best practices (PHP)
PPTX
Standards: Don't pee in the pool
PPTX
Software Development with PHP & Laravel
PDF
PSR-7 and PSR-15, why can't you ignore them
ODP
PHP Code Quality
PDF
PHP Development Tools
PDF
WordPress Development in a Modern PHP World
PDF
"PHP Code Avengers: Structuring, Refactoring, and Superhero Practices", Vlad...
PPTX
Essential Tools for Modern PHP
PDF
Composer Helpdesk
PDF
WordPress Development in a Modern PHP World
Current status of PSR - Phpblt1
Php psr standard 2014 01-22
Meet up symfony 16 juin 2017 - Les PSR
Submit PHP: Standards in PHP world. Михайло Морозов
PHP CODING STANDARDS
Psr 2 coding style guide - Tidepool Labs
Let's PHP in a better way! - Coding Recommendations.
2021.laravelconf.tw.slides2
Making the Most of Modern PHP in Drupal 7
Programming best practices (PHP)
Standards: Don't pee in the pool
Software Development with PHP & Laravel
PSR-7 and PSR-15, why can't you ignore them
PHP Code Quality
PHP Development Tools
WordPress Development in a Modern PHP World
"PHP Code Avengers: Structuring, Refactoring, and Superhero Practices", Vlad...
Essential Tools for Modern PHP
Composer Helpdesk
WordPress Development in a Modern PHP World
Ad

Recently uploaded (20)

PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PPTX
UNIT 4 Total Quality Management .pptx
PPTX
Welding lecture in detail for understanding
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PPT
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PPTX
bas. eng. economics group 4 presentation 1.pptx
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PDF
composite construction of structures.pdf
PDF
Digital Logic Computer Design lecture notes
PDF
PPT on Performance Review to get promotions
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PPTX
Internet of Things (IOT) - A guide to understanding
PDF
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
PPTX
OOP with Java - Java Introduction (Basics)
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PPT
Project quality management in manufacturing
PPTX
web development for engineering and engineering
PPTX
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
UNIT 4 Total Quality Management .pptx
Welding lecture in detail for understanding
Embodied AI: Ushering in the Next Era of Intelligent Systems
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
Model Code of Practice - Construction Work - 21102022 .pdf
bas. eng. economics group 4 presentation 1.pptx
Operating System & Kernel Study Guide-1 - converted.pdf
composite construction of structures.pdf
Digital Logic Computer Design lecture notes
PPT on Performance Review to get promotions
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
Internet of Things (IOT) - A guide to understanding
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
OOP with Java - Java Introduction (Basics)
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
Project quality management in manufacturing
web development for engineering and engineering
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
Ad

PHP Standards Recommendations - PHP-FIG

  • 2. – Martin Fowler “Any fool can write code that a computer can understand. Good programmers write code that humans can understand.”
  • 3. PSR-1: Basic Coding Standards. • MUST use only <?php and <?= tags. • MUST use only UTF-8 Encoding. • SHOULD either declare symbols or cause side-effects. • Class names MUST be declared in CamelCase or StudlyCaps. • constants MUST be declared in all UPPER_SNAKE_CASE. • Method names MUST be declared in camelCase.
  • 4. Quiz Time: What is wrong with following code snippets?
  • 5. PSR-2: Coding Style Guide • 4 Space indenting (No Tabs). • Soft Line limit 120 Characters. • Blank space after namespace declaration before use. • Closing Brace “}” always on a separate line. • Opening Brace “{” Same line (with a space before) for control statements and next line for Classes and functions. • Space before open parentheses “(” for control statements and not for functions. • Visibility mode is must for all properties and methods. abstract and final before visibility and static after the visibility.
  • 6. Quiz Time: What is wrong with following code snippets?
  • 7. PSR-3: Logger Interface • Only allowed Log levels: debug, info, notice, warning, error, critical, alert, emergency. • MUST implement PsrLogLoggerInterface. • MUST throw PsrLogInvalidArgumentException exception on invalid log level. • public function log($level, $message, array $context = array()); • One can consider extending PsrLogAbstractLogger class which implements LoggerInterface.
  • 9. PSR-4: Autoloader • Namespace: <vendor_namespace>/ <package_namespace>[/<subnamespace>/]/ <class_name>. • Folder structure should also follow the namespace pattern.
  • 10. PSR-6: Caching Interface • MUST implement PsrCacheCacheItemPoolInterface and PsrCacheCacheItemInterface. • Concepts: Pool, Items, Key, Data, TTL, Hit, Miss. • Should not throw any Exception.
  • 11. PSR-7: HTTP message interfaces • Request Structure • Response Structure • Interfaces: PsrHttpMessageRequestInterface and PsrHttpMessageResponseInterface.
  • 12. PSR-11: Container interface • MUST implement PsrContainerContainerInterface. • MUST throw PsrContainerNotFoundExceptionInterface exception if object not found. • Directly thrown exceptions must implement PsrContainerContainerExceptionInterface interface.
  • 13. Standards that most of the developers follow. • If a file defines a Class then it should not have any other declarations though PSR allows. • Files with functions should have only function definitions. • Files with class / functions definitions should not have a closing PHP Tag (?>) and should have an empty line before EOF. • Helper files must have function_exists check. • Code MUST be correctly formatted based on the PSR rules. (editor plugins can be used for the same). • Arrays properties should be on separate lines. • All declarations MUST have dock-blocks. • Branch names MUST follow bitbucket Branching Model.
  • 14. Cont. • Use static instead of self for calling static class members. • Use die instead of exit. • Make use of destructors for unsetting objects and/or closing opened files. • There should not be any commented code or commented die / dd statements. • SHOULD avoid else block in case of if statements. • Recommended to make use of exceptions in-case of invalid details which should be handles at calling end. • Try to use User defined constants instead of System constants. A constants class should be created for defining all the constants that are being used in the project. E.g. instead of using “/” for directory paths use DIRECTORY_SEPARATOR or instead of using supplier_id == 7 check use supplier_id == Constants::DELHIVERY_SUPPLIER_ID. • PHP Unit Test cases are compulsory.