PSR-1, 4, and 12 MUST be followed
- PSR-1: Basic Coding Standard: https://www.php-fig.org/psr/psr-1/
- PSR-4: Autoloader: https://www.php-fig.org/psr/psr-4/
- PSR-12: Extended Coding Style: https://www.php-fig.org/psr/psr-12/
PHP Standards Recommendations: https://www.php-fig.org/psr/.
- Since PHP 5.4: Yes
- Code more readable and maintainable
- https://wiki.php.net/rfc/shortsyntaxforarrays
- Interfaces MUST be suffixed by
Interface
: e.g.Psr\Foo\BarInterface
- Abstract classes MUST be prefixed by
Abstract
: e.g.Psr\Foo\AbstractBar
- Traits MUST be suffixed by Trait: e.g.
Psr\Foo\BarTrait
- PSR-1, 4, and 12 MUST be followed
- For code released as part of a PSR, the vendor namespace MUST be
Psr
and the Composer package name MUST bepsr/<package>
(e.g.,psr/log
) - For code released as part of a PER or any other Auxiliary Resources, the vendor namespace MUST be Fig and the Composer package name MUST be
fig/<package>
(e.g.,fig/cache-util
) - There MUST be a package/second-level namespace in relation with the PSR or PER that covers the code
- Implementations of a given PSR or PER SHOULD declare a
provides
key in theircomposer.json file
in the formpsr/<package>-implementation
with a version number that matches the PSR being implemented. For example,"psr/<package>-implementation": "1.0.0"
https://www.php-fig.org/bylaws/psr-naming-conventions/
PSR-1: Basic Coding Standard (Excerpt)
- 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
- PHP code MUST use the long
<?php ?>
tags or the short-echo<?= ?>
tags; it MUST NOT use the other tag variations
PSR-4: Autoloader (Excerpt)
- A fully qualified class name has the following form:
\<NamespaceName>(\<SubNamespaceNames>)*\<ClassName>
- Underscores have no special meaning in any portion of the fully qualified class name
- All class names MUST be referenced in a case-sensitive fashion
PSR-12: Extended Coding Style (Excerpt)
- Files
- The closing
?>
tag MUST be omitted from files containing only PHP
- The closing
- Lines
- There MUST NOT be a hard limit on line length
- The soft limit on line length MUST be 120 characters
- Lines SHOULD NOT be longer than 80 characters; lines longer than that SHOULD be split into multiple subsequent lines of no more than 80 characters each
- Blank lines MAY be added to improve readability and to indicate related blocks of code except where explicitly forbidden
- There MUST NOT be more than one statement per line
- Indenting
- Code MUST use an indent of 4 spaces for each indent level, and MUST NOT use tabs for indenting
- Keywords and Types