Skip to content

Instantly share code, notes, and snippets.

@Muetze42
Last active July 14, 2023 10:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Muetze42/827cc1b4ee3adc010dd9c57abab952d0 to your computer and use it in GitHub Desktop.
Save Muetze42/827cc1b4ee3adc010dd9c57abab952d0 to your computer and use it in GitHub Desktop.
PHP Standards

PHP Standards

PSR-1, 4, and 12 MUST be followed

PSR Naming Conventions (4)

PHP Standards Recommendations: https://www.php-fig.org/psr/.

Array: Short syntax for arrays?

PSR Naming Conventions

  • 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 be psr/<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 their composer.json file in the form psr/<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/


Coding Standard Excerpts

  • 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
  • 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
  • Files
    • The closing ?> tag MUST be omitted from files containing only PHP
  • 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
    • PSR-12: All PHP reserved keywords and types [1][2] MUST be in lower case
    • PSR-12: Any new types and keywords added to future PHP versions MUST be in lower case.
    • PSR-12: Short form of type keywords MUST be used i.e. bool instead of boolean, int instead of integer etc.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment