Joe's
Digital Garden

PHP Code Style

The key items from PSR1 and PSR121 that I find useful:

  • Files should either declare symbols or cause side-effects, but not both
  • Declare classnames with StudlyCaps
  • Constants are in UPPER_CASE_SNAKE
  • Methods are camelCase
  • One class per file, under a vendored namespace
  • One statement per line
  • Use UNIX LF for line endings with no trailing whitespace
  • Omit the closing ?> from files containing only PHP
  • Soft line limit of 120 character, but try to avoid going over 80
  • Keywords always in lowercase (if, for, true)
  • Order of the file header is: php-tag, declare statements, namespace, class imports, function imports, contsnt imports
  • Always use strict types (declare(strict_types=1)) and declare return types and typehint parameters when possible
  • Extends and implements are on the same line as the class name or one per line. Opening bracket for a class is on a new line
  • Trait use statements are the first line of the class and are on their own line
  • Class properties must have visiblity declared and are one per line after trait statements
  • Method visibility must be declared, opening bracket on new line, arguments split across multiple lines if they break the 80 character limit
  • Classes are ordered by trait, properties, methods and within each category they are alphabatized
  • No spaces between parantheses (if ($expr)), but yes to spaces between brackets and control structures (if (true) {). Control structure brackets stay on the same line. Use the if (true): and endif syntax in mixed php/html files.
  • Spaces between arrows (foreach ($iterable as $key => $value) {)
  • Always include a space after function

I personally also like to align the arrows in large associative arrays or blocks:

$largeArray = [
    'key1'     => 'val1',
    'longKey2' => 'val2'
];

Order of Constants, Properties and Methods

Although not an official part of PSR-12 because the final decision was to allow individual projects flexibility2. The following is a general good practice for ordering constants, properties and methods within PHP classes.

 In PHP de-facto exists some best practice of ordering methods and  properties. 
 It seems to me that this order is most common:

 Constants
    public const
    protected const
    private const
 Properties
     public static properties
    public properties
    protected static properties
    protected properties
    private static properties
    private properties
 Methods
    magic methods
    public static methods
    public methods
    protected static methods
    protected methods
    private static methods
    private methods

External References

  1. PSR-12: Extended Coding Style<https://www.php-fig.org/psr/psr-12/>. PHP-FIG. Retrieved 2022-06-30.
  2. Фрейман, Кирилл. PSR-12: Order of сonstants, methods and properties definitions<https://www.mail-archive.com/php-fig@googlegroups.com/msg02290.html>. The Mail Archive. Retrieved 2022-06-30.

Linked References