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’);