PHP Standards Recommendations [PSR-2]

# General

  • Code MUST follow a "coding style guide" PSR [PSR-1]
<?php
namespace Vendor\Model;

class Foo
{
    const VERSION = '1.0';
    const DATE_APPROVED = '2019-06-01';

    public function fooBar()
    {
        $barBaz = 'foo';

        return $barBaz;
    }
}
  • All PHP files MUST end with a single blank line.
  • The closing ?> tag MUST be deleted from files containing only PHP.
  • 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.
  • There MUST NOT be trailing whitespaces at the end of non-blank lines.
  • Blank lines MAY be added to improve readability and to indicate related blocks of code.
  • There MUST NOT be more than one statement per line.
  • Code MUST use 4 spaces for indenting, not tabs.
  • PHP keywords MUST be in lower case.
  • The PHP constants true, false, and null MUST be in lower case.

# Namespaces and Use Declarations

  • There MUST be one blank line after the namespace declaration.
  • All use declarations MUST go after namespace declaration.
  • There MUST be one use keyword per declaration.
  • There MUST be one blank line after the block of use declarations.
<?php
namespace Vendor\Model;

use FooInterface;
use BarClass as Bar;
use OtherVendor\OtherPackage\BazClass;

// additional PHP code...

# Classes, Properties, and Methods

  • The extends and implements keywords MUST be declared on the same line as the class name.
  • Opening braces for classes MUST go on the next line, and closign braces MUST go on the next line after the body.
  • Lists of implements MAY be split across multiple lines, where each subsequent line is indented once. When doing so, the first item in the list MUST be on the next line, and there MUST be only one interface per line.
<?php
namespace Vendor\Package;

use FooClass;
use BarClass as Bar;
use OtherVendor\OtherPackage\BazClass;

class ClassName extends ParentClass implements
    \ArrayAccess,
    \Countable,
    \Serializable
{
    // constants, properties, methods
}
  • Visibility MUST be declared on all properties.
  • There MUST NOT be more than one property declared per statement.
  • Property names SHOULD NOT be prefixed with a single underscore to indicate protected or private visibility.
<?php
namespace Vendor\Package;

class ClassName
{
    public $foo = null;
}
  • Visibility MUST be declared on all methods.
  • Method names SHOULD NOT be prefixed with a single underscore to indicate protected or private visibility.
  • Opening braces for the methods MUST go on the next line, and closing braces MUST go on the next line after the body.
  • Method names MUST NOT be declared with a space after the method name.
  • There MUST NOT be a space after the opening parenthesis, and there MUST NOT be a space before the closing parenthesis.
  • There MUST NOT be a space before each comma, and there MUST be one space after each comma.
  • Method arguments with default values MUST go at the end of the argument list.

A method declaration looks like the following. Note the placement of parentheses, commas, spaces, and braces:

<?php
namespace Vendor\Package;

class ClassName
{
    public function fooBarBaz($arg1, &$arg2, $arg3 = [])
    {
        // method body
    }
}
  • Argument lists MAY be split across multiple lines. When doing so, there MUST be only one argument per line.
<?php
namespace Vendor\Package;

class ClassName
{
    public function aVeryLongMethodName(
        ClassTypeHint $arg1,
        &$arg2,
        array $arg3 = []
    ) {
        // method body
    }
}
  • abstract and final MUST be declared on before the visibility
  • static MUST be declared after the visibility.
<?php
namespace Vendor\Package;

abstract class ClassName
{
    protected static $foo;

    abstract protected function zim();

    final public static function bar()
    {
        // method body
    }
}

# Control Structures

  • Control structure keywords MUST have one space after them
  • Opening braces for the control structures MUST go on the same line, and closing braces MUST go on the next line after the body.
  • Opening parentheses for control structures MUST NOT have space after them,and closing parentheses for control structures MUST NOT have space before.
  • There MUST be one space between the closing parenthesis and the opening brace.
  • The structure body MUST be indented once.
  • An if structure looks like the following. Note the placement of parentheses, spaces, and braces.
<?php
if ($expr1) {
    // if body
} elseif ($expr2) {
    // elseif body
} else {
    // else body;
}
  • A switch structure looks like the following.
<?php
switch ($expr) {
    case 0:
        echo 'First case, with a break';
        break;
    case 1:
        echo 'Second case, which falls through';
        // no break
    case 2:
    case 3:
    case 4:
        echo 'Third case, return instead of break';
        return;
    default:
        echo 'Default case';
        break;
}
  • A while statement looks like the following.
<?php
while ($expr) {
    // structure body
}
  • A do while statement looks like the following.
<?php
do {
    // structure body;
} while ($expr);
  • A for statement looks like the following.
<?php
for ($i = 0; $i < 10; $i++) {
    // for body
}
  • A foreach statement looks like the following.
<?php
foreach ($iterable as $key => $value) {
    // foreach body
}
  • A try catch block looks like the following.
<?php
try {
    // try body
} catch (FirstExceptionType $e) {
    // catch body
} catch (OtherExceptionType $e) {
    // catch body
}

# Closures

  • Closures MUST be declared with a space after the function keyword, and a space before and after the use keyword.
  • The opening brace MUST go on the same line, and the closing brace MUST go on the next line following the body.
  • There MUST NOT be a space after the opening parenthesis of the argument list or variable list, and there MUST NOT be a space before the closing parenthesis of the argument list or variable list.
  • In the argument list and variable list, there MUST NOT be a space before each comma, and there MUST be one space after each comma.
  • Closure arguments with default values MUST go at the end of the argument list.
<?php
$closureWithArgs = function ($arg1, $arg2) {
    // body
};

$closureWithArgsAndVars = function ($arg1, $arg2) use ($var1, $var2) {
    // body
};
  • Argument lists and variable lists MAY be split across multiple lines.
<?php

// Note:
// arguments: the data you pass into the method's params.
$longArgs_noVars = function (
    $longArgument,
    $longerArgument,
    $muchLongerArgument
) {
    // body
};

$noArgs_longVars = function () use (
    $longVar1,
    $longerVar2,
    $muchLongerVar3
) {
    // body
};

$longArgs_longVars = function (
    $longArgument,
    $longerArgument,
    $muchLongerArgument
) use (
    $longVar1,
    $longerVar2,
    $muchLongerVar3
) {
    // body
};

$longArgs_shortVars = function (
    $longArgument,
    $longerArgument,
    $muchLongerArgument
) use ($var1) {
    // body
};

$shortArgs_longVars = function ($arg) use (
    $longVar1,
    $longerVar2,
    $muchLongerVar3
) {
    // body
};
PHP#PHPOOP#OOP

Comments (2)

Mohamed Samir
Mohamed Samir
7 months ago

thank you

Mohamed Kamal
Mohamed Kamal
7 months ago

Great effort