SlideShare una empresa de Scribd logo
1 de 30
Descargar para leer sin conexión
Coding Standards by Aram Baghdasaryan
Coding Standards
PSR-1 & PSR-2
Coding Standards by Aram Baghdasaryan
PHP Standards Recommendation
What is PSR?
Coding Standards by Aram Baghdasaryan
PHP Standards Recommendation
PSR-0 - Autoloader Standard
What is PSR?
Coding Standards by Aram Baghdasaryan
PHP Standards Recommendation
PSR-0 - Autoloader Standard
PSR-1 - Basic Coding Standard
What is PSR?
Coding Standards by Aram Baghdasaryan
PHP Standards Recommendation
PSR-0 - Autoloader Standard
PSR-1 - Basic Coding Standard
PSR-2 - Coding Style Guide
What is PSR?
Coding Standards by Aram Baghdasaryan
PHP Standards Recommendation
PSR-0 - Autoloader Standard
PSR-1 - Basic Coding Standard
PSR-2 - Coding Style Guide
PSR-3 - Logger Interface
What is PSR?
Coding Standards by Aram Baghdasaryan
PHP Standards Recommendation
PSR-0 - Autoloader Standard
PSR-1 - Basic Coding Standard
PSR-2 - Coding Style Guide
PSR-3 - Logger Interface
PSR-4 - Autoloader Standard
What is PSR?
Coding Standards by Aram Baghdasaryan
Single style guide for PHP code that results
in uniformly formatted shared code
What it gives to us?
Coding Standards by Aram Baghdasaryan
· Files MUST use only <?php and <?= tags
· Files MUST use only UTF-8 without BOM for
PHP code.
· Files SHOULD either declare symbols or cause
side-effects but SHOULD NOT do both
PSR-1 Overview
Coding Standards by Aram Baghdasaryan
Files SHOULD either declare
symbols or cause side-effects
but SHOULD NOT do both
Coding Standards by Aram Baghdasaryan
// side effect: change ini settings
ini_set('error_reporting', E_ALL);
// side effect: loads a file
include "file.php";
Files SHOULD either declare
symbols or cause side-effects
but SHOULD NOT do both
Coding Standards by Aram Baghdasaryan
· Namespaces and classes MUST follow an
"autoloading" PSR
· Class names MUST be declared in StudlyCaps
· Class constants MUST be declared in all upper
case with underscore separators.
· Method names MUST be declared in camelCase
PSR-1 Overview
Coding Standards by Aram Baghdasaryan
· Code MUST follow a "coding style guide" PSR-1
· Code MUST use 4 spaces for indenting, not tabs
· There MUST NOT be a hard limit on line length,
the soft limit MUST be 120 characters, lines
SHOULD be 80 characters or less
PSR-2 Overview
Coding Standards by Aram Baghdasaryan
· There MUST be one blank line after the
namespace declaration, and there MUST be one
blank line after the block of use declarations
· Opening braces for classes MUST go on the next
line, and closing braces MUST go on the next line
after the body
PSR-2 Overview
Coding Standards by Aram Baghdasaryan
<?php
namespace App;
use VendorLib1Tool;
use VendorLib2Tool;
class BloBlo {
...
}
Coding Standards by Aram Baghdasaryan
· Opening braces for methods MUST go on the
next line, and closing braces MUST go on the
next line after the body
· Visibility MUST be declared on all properties
and methods, abstract and final MUST be
declared before the visibility, static MUST be
declared after the visibility
PSR-2 Overview
Coding Standards by Aram Baghdasaryan
final public static function getBlo()
{
...
}
Coding Standards by Aram Baghdasaryan
· Control structure keywords MUST have one
space after them, method and function calls
MUST NOT
· Opening braces for control structures MUST go
on the same line, and closing braces MUST go on
the next line after the body
PSR-2 Overview
Coding Standards by Aram Baghdasaryan
· Opening parentheses for control structures
MUST NOT have a space after them, and closing
parentheses for control structures MUST NOT
have a space before
PSR-2 Overview
Coding Standards by Aram Baghdasaryan
function getBlo($param) use ($other)
{
if (true) {
...
}
}
getBlo(‘value’);
Coding Standards by Aram Baghdasaryan
Other Examples
Coding Standards by Aram Baghdasaryan
function aLongMethodName(
ClassTypeHint $arg1,
&$arg2,
array $arg3 = []
) {
// method body
}
Coding Standards by Aram Baghdasaryan
$foo->bar(
$longArgument,
$longerArgument,
$muchLongerArgument
);
Coding Standards by Aram Baghdasaryan
if ($expr1) {
// if body
} elseif ($expr2) {
// elseif body
} else {
// else body
}
Coding Standards by Aram Baghdasaryan
switch ($expr) {
case 0:
echo 'First case, with a break';
break;
case 2:
case 3:
echo 'Third case';
return;
default:
echo 'Default case';
}
Coding Standards by Aram Baghdasaryan
while ($expr) {
// structure body
}
do {
// structure body;
} while ($expr);
Coding Standards by Aram Baghdasaryan
for ($i = 0; $i < 10; $i++) {
// for body
}
foreach ($iterable as $key => $value) {
// foreach body
}
Coding Standards by Aram Baghdasaryan
try {
// try body
} catch (FirstExceptionType $e) {
// catch body
} catch (OtherExceptionType $e) {
// catch body
}
Coding Standards by Aram Baghdasaryan
$closure = function ($arg1, $arg2) {
// body
};
$closure = function ($arg1) use ($var1) {
// body
};
Coding Standards by Aram Baghdasaryan
Thank You!

Más contenido relacionado

La actualidad más candente

La actualidad más candente (20)

C if else
C if elseC if else
C if else
 
Bitwise operators
Bitwise operatorsBitwise operators
Bitwise operators
 
Python programming : Abstract classes interfaces
Python programming : Abstract classes interfacesPython programming : Abstract classes interfaces
Python programming : Abstract classes interfaces
 
HTML & CSS: Chapter 06
HTML & CSS: Chapter 06HTML & CSS: Chapter 06
HTML & CSS: Chapter 06
 
Abstraction in c++ and Real Life Example of Abstraction in C++
Abstraction in c++ and Real Life Example of Abstraction in C++Abstraction in c++ and Real Life Example of Abstraction in C++
Abstraction in c++ and Real Life Example of Abstraction in C++
 
Computer programming
Computer programmingComputer programming
Computer programming
 
C++ programming function
C++ programming functionC++ programming function
C++ programming function
 
System Programming :: Assembler
System Programming :: AssemblerSystem Programming :: Assembler
System Programming :: Assembler
 
Asymptotic Notation
Asymptotic NotationAsymptotic Notation
Asymptotic Notation
 
Minicurso PHP básico
Minicurso PHP básicoMinicurso PHP básico
Minicurso PHP básico
 
Pointers in C/C++ Programming
Pointers in C/C++ ProgrammingPointers in C/C++ Programming
Pointers in C/C++ Programming
 
Introduction to systems programming
Introduction to systems programmingIntroduction to systems programming
Introduction to systems programming
 
If else statement in c++
If else statement in c++If else statement in c++
If else statement in c++
 
Compiler vs Interpreter-Compiler design ppt.
Compiler vs Interpreter-Compiler design ppt.Compiler vs Interpreter-Compiler design ppt.
Compiler vs Interpreter-Compiler design ppt.
 
Unit 7. Functions
Unit 7. FunctionsUnit 7. Functions
Unit 7. Functions
 
python Function
python Function python Function
python Function
 
Nesting of if else statement & Else If Ladder
Nesting of if else statement & Else If Ladder Nesting of if else statement & Else If Ladder
Nesting of if else statement & Else If Ladder
 
Introduction to Python Basics Programming
Introduction to Python Basics ProgrammingIntroduction to Python Basics Programming
Introduction to Python Basics Programming
 
Functions ppt ch06
Functions ppt ch06Functions ppt ch06
Functions ppt ch06
 
Functions in c language
Functions in c language Functions in c language
Functions in c language
 

Destacado

Введение в Cаентологическую Этику
Введение в Cаентологическую ЭтикуВведение в Cаентологическую Этику
Введение в Cаентологическую Этику
web-processing
 
Maturing_Project_Mgmt_Practice_at_National_R&D Lab
Maturing_Project_Mgmt_Practice_at_National_R&D LabMaturing_Project_Mgmt_Practice_at_National_R&D Lab
Maturing_Project_Mgmt_Practice_at_National_R&D Lab
Keith Motyl
 
Japanese fishing story 130818233933-phpapp01
Japanese fishing story 130818233933-phpapp01Japanese fishing story 130818233933-phpapp01
Japanese fishing story 130818233933-phpapp01
Viren Sharma
 
Untitled Presentation
Untitled PresentationUntitled Presentation
Untitled Presentation
Monica Bonino
 

Destacado (17)

PHP CODING STANDARDS
PHP CODING STANDARDSPHP CODING STANDARDS
PHP CODING STANDARDS
 
Cheap Vacation Trips For Summer
Cheap Vacation Trips For SummerCheap Vacation Trips For Summer
Cheap Vacation Trips For Summer
 
Welding equipment | #Weldingequipment
Welding equipment | #WeldingequipmentWelding equipment | #Weldingequipment
Welding equipment | #Weldingequipment
 
Введение в Cаентологическую Этику
Введение в Cаентологическую ЭтикуВведение в Cаентологическую Этику
Введение в Cаентологическую Этику
 
Absolutevaluelab
AbsolutevaluelabAbsolutevaluelab
Absolutevaluelab
 
N.Gencevi
N.GenceviN.Gencevi
N.Gencevi
 
Vancouver SEO Services
Vancouver SEO ServicesVancouver SEO Services
Vancouver SEO Services
 
Gaining Strategic Insight For Higher Profits
Gaining Strategic Insight For Higher ProfitsGaining Strategic Insight For Higher Profits
Gaining Strategic Insight For Higher Profits
 
SEO In Vancouver
SEO In VancouverSEO In Vancouver
SEO In Vancouver
 
Maturing_Project_Mgmt_Practice_at_National_R&D Lab
Maturing_Project_Mgmt_Practice_at_National_R&D LabMaturing_Project_Mgmt_Practice_at_National_R&D Lab
Maturing_Project_Mgmt_Practice_at_National_R&D Lab
 
My Portfolio
My PortfolioMy Portfolio
My Portfolio
 
Virtual private networks by darshana viduranga
Virtual private networks by darshana vidurangaVirtual private networks by darshana viduranga
Virtual private networks by darshana viduranga
 
Japanese fishing story 130818233933-phpapp01
Japanese fishing story 130818233933-phpapp01Japanese fishing story 130818233933-phpapp01
Japanese fishing story 130818233933-phpapp01
 
2015-16 software project list
2015-16 software project list2015-16 software project list
2015-16 software project list
 
Untitled Presentation
Untitled PresentationUntitled Presentation
Untitled Presentation
 
Portfolio
PortfolioPortfolio
Portfolio
 
ДИАНЕТИКА – Современная наука душевного здоровья
ДИАНЕТИКА – Современная наука душевного здоровьяДИАНЕТИКА – Современная наука душевного здоровья
ДИАНЕТИКА – Современная наука душевного здоровья
 

Último

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 

Último (20)

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 

Coding standards PSR-1 & PSR-2

  • 1. Coding Standards by Aram Baghdasaryan Coding Standards PSR-1 & PSR-2
  • 2. Coding Standards by Aram Baghdasaryan PHP Standards Recommendation What is PSR?
  • 3. Coding Standards by Aram Baghdasaryan PHP Standards Recommendation PSR-0 - Autoloader Standard What is PSR?
  • 4. Coding Standards by Aram Baghdasaryan PHP Standards Recommendation PSR-0 - Autoloader Standard PSR-1 - Basic Coding Standard What is PSR?
  • 5. Coding Standards by Aram Baghdasaryan PHP Standards Recommendation PSR-0 - Autoloader Standard PSR-1 - Basic Coding Standard PSR-2 - Coding Style Guide What is PSR?
  • 6. Coding Standards by Aram Baghdasaryan PHP Standards Recommendation PSR-0 - Autoloader Standard PSR-1 - Basic Coding Standard PSR-2 - Coding Style Guide PSR-3 - Logger Interface What is PSR?
  • 7. Coding Standards by Aram Baghdasaryan PHP Standards Recommendation PSR-0 - Autoloader Standard PSR-1 - Basic Coding Standard PSR-2 - Coding Style Guide PSR-3 - Logger Interface PSR-4 - Autoloader Standard What is PSR?
  • 8. Coding Standards by Aram Baghdasaryan Single style guide for PHP code that results in uniformly formatted shared code What it gives to us?
  • 9. Coding Standards by Aram Baghdasaryan · Files MUST use only <?php and <?= tags · Files MUST use only UTF-8 without BOM for PHP code. · Files SHOULD either declare symbols or cause side-effects but SHOULD NOT do both PSR-1 Overview
  • 10. Coding Standards by Aram Baghdasaryan Files SHOULD either declare symbols or cause side-effects but SHOULD NOT do both
  • 11. Coding Standards by Aram Baghdasaryan // side effect: change ini settings ini_set('error_reporting', E_ALL); // side effect: loads a file include "file.php"; Files SHOULD either declare symbols or cause side-effects but SHOULD NOT do both
  • 12. Coding Standards by Aram Baghdasaryan · Namespaces and classes MUST follow an "autoloading" PSR · Class names MUST be declared in StudlyCaps · Class constants MUST be declared in all upper case with underscore separators. · Method names MUST be declared in camelCase PSR-1 Overview
  • 13. Coding Standards by Aram Baghdasaryan · Code MUST follow a "coding style guide" PSR-1 · Code MUST use 4 spaces for indenting, not tabs · There MUST NOT be a hard limit on line length, the soft limit MUST be 120 characters, lines SHOULD be 80 characters or less PSR-2 Overview
  • 14. Coding Standards by Aram Baghdasaryan · There MUST be one blank line after the namespace declaration, and there MUST be one blank line after the block of use declarations · Opening braces for classes MUST go on the next line, and closing braces MUST go on the next line after the body PSR-2 Overview
  • 15. Coding Standards by Aram Baghdasaryan <?php namespace App; use VendorLib1Tool; use VendorLib2Tool; class BloBlo { ... }
  • 16. Coding Standards by Aram Baghdasaryan · Opening braces for methods MUST go on the next line, and closing braces MUST go on the next line after the body · Visibility MUST be declared on all properties and methods, abstract and final MUST be declared before the visibility, static MUST be declared after the visibility PSR-2 Overview
  • 17. Coding Standards by Aram Baghdasaryan final public static function getBlo() { ... }
  • 18. Coding Standards by Aram Baghdasaryan · Control structure keywords MUST have one space after them, method and function calls MUST NOT · Opening braces for control structures MUST go on the same line, and closing braces MUST go on the next line after the body PSR-2 Overview
  • 19. Coding Standards by Aram Baghdasaryan · Opening parentheses for control structures MUST NOT have a space after them, and closing parentheses for control structures MUST NOT have a space before PSR-2 Overview
  • 20. Coding Standards by Aram Baghdasaryan function getBlo($param) use ($other) { if (true) { ... } } getBlo(‘value’);
  • 21. Coding Standards by Aram Baghdasaryan Other Examples
  • 22. Coding Standards by Aram Baghdasaryan function aLongMethodName( ClassTypeHint $arg1, &$arg2, array $arg3 = [] ) { // method body }
  • 23. Coding Standards by Aram Baghdasaryan $foo->bar( $longArgument, $longerArgument, $muchLongerArgument );
  • 24. Coding Standards by Aram Baghdasaryan if ($expr1) { // if body } elseif ($expr2) { // elseif body } else { // else body }
  • 25. Coding Standards by Aram Baghdasaryan switch ($expr) { case 0: echo 'First case, with a break'; break; case 2: case 3: echo 'Third case'; return; default: echo 'Default case'; }
  • 26. Coding Standards by Aram Baghdasaryan while ($expr) { // structure body } do { // structure body; } while ($expr);
  • 27. Coding Standards by Aram Baghdasaryan for ($i = 0; $i < 10; $i++) { // for body } foreach ($iterable as $key => $value) { // foreach body }
  • 28. Coding Standards by Aram Baghdasaryan try { // try body } catch (FirstExceptionType $e) { // catch body } catch (OtherExceptionType $e) { // catch body }
  • 29. Coding Standards by Aram Baghdasaryan $closure = function ($arg1, $arg2) { // body }; $closure = function ($arg1) use ($var1) { // body };
  • 30. Coding Standards by Aram Baghdasaryan Thank You!