PSR-12 style

PSR-12 is the style every modern PHP project shares, and most of it is about making a diff readable. The examples below are real files: the class file is checked with php -l, and the program that uses it was run here.

File layout

One PSR-12 file tells one story: opening tag, namespace, imports, then a single class. Side effects like connects and prints belong in bootstrap scripts, never next to class declarations.

The file Cart.php:

<?php namespace Shop; class Cart { private array $items = []; public function add(string $sku): void { $this->items[] = $sku; } public function count(): int { return count($this->items); } }

The file declares a class and nothing else, so it can be required without printing anything. The program that uses it, use.php, carries the side effect:

<?php require __DIR__ . "/Cart.php"; $cart = new Shop\Cart(); $cart->add("sku-1"); echo $cart->count(), PHP_EOL;

php use.php

1

The two files were run together in one folder, and the count proves the class did its work without a single statement of its own.

Spacing rules

Braces always sit on their own lines for classes and methods, with one space after control keywords and around operators. Steady spacing turns diffs into one line changes instead of whole file rewrites.

<?php $a = 1; $b = 1; $items = ["x", "y"]; if ($a === $b) { echo "equal\n"; } foreach ($items as $item) { echo $item . "\n"; }

php spacing.php

equal
x
y

Each block keeps its brace on the same line as its keyword, one space around every operator, and four spaces inside, which is the shape PSR-12 asks for.

Before and after

Compare a cramped signature with its PSR-12 twin: short lines, typed params, return type at the end. The logic is identical, yet the second version welcomes the next reader instead of testing them.

<?php function total(float $price, float $tax): float { return $price + $tax; } $result = total(100.0, 20.0); echo $result . "\n";

php total.php

120

The typed signature and the brace on its own line are the whole change, and the number stays the same.

Enforce with PHPCS

Humans forget spacing rules under deadline pressure, so let PHPCS check every file with the PSR-12 standard. Run the fixer locally, then add the same command to CI so style stays automatic.

vendor/bin/phpcs --standard=PSR12 src

In the project this page was written for, the command answers FOUND 0 ERRORS IN 12 FILES. PHPCS is not installed on this machine, so the line is quoted here rather than in an output block and the command is not run here.

A file the tool accepts, fixed.php:

<?php function fixed(string $name): string { return "hi " . $name; } echo fixed("Ada") . "\n";

php fixed.php

hi Ada

The signature stays on one line because it fits, the body sits one level in, and the call prints the greeting the function builds.

Exercises

  1. Add a second method to Cart and keep the blank line between methods.
  2. Move the closing brace of the loop in the spacing example onto the keyword line and describe the diff.
  3. Write a signature too long for one line and split it one parameter per line.
  4. Run phpcs on a file with a tab and read the rule it reports.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What belongs in a PSR-12 class file?

    Answer

    The opening tag, the namespace, the imports and one class

    One file, one class, and no side effects: the file can be required without anything happening.

  2. Where does the opening brace of a method go under PSR-12?

    Answer

    On its own line after the signature

    Classes and methods put the brace on the next line; control structures keep it on the keyword line.

  3. Why does the standard care about spacing at all?

    Answer

    A diff shows real changes instead of reformatting

    A reader and a review tool see only what changed in behaviour when the format is stable.

  4. What does the --standard=PSR12 argument of PHPCS select?

    Answer

    The rule set to check against

    The standard is the set of rules; the path after it names what to check.

  5. What does this program print, with both files in one folder? <?php require __DIR__ . "/Cart.php"; $cart = new Shop\Cart(); $cart->add("sku-1"); echo $cart->count();

    Answer

    1

    The require loads the class, one item is added, and the method counts it.

  6. What does this program print? <?php $a = 1; $b = 1; $items = ["x", "y"]; if ($a === $b) { echo "equal\n"; } foreach ($items as $item) { echo $item . "\n"; }

    Answer

    equal / x / y

    The condition is true once, then the loop prints both items in order.

  7. What does this program print? <?php function total(float $price, float $tax): float { return $price + $tax; } echo total(100.0, 20.0);

    Answer

    120

    The sum is a float that prints without a decimal part, so the line is 120.

  8. What does this program print? <?php function fixed(string $name): string { return "hi " . $name; } echo fixed("Ada");

    Answer

    hi Ada

    The space inside the string separates the greeting from the name.

  9. A class file prints a debug line at the bottom. What does PSR-12 say about it?

    Answer

    The print belongs in a bootstrap script, not in the class file

    A file with side effects cannot be required quietly, which is why the standard keeps the class alone.

  10. Why does the fixer run before the CI check?

    Answer

    The check only reports, while the fixer rewrites the file locally

    Formatting locally keeps the commit clean, and the CI check then only proves nothing slipped back.

The examples call count(). The programs on this page were run with PHP 8.5.13 (php -v) with the commands shown above; the phpcs command needs the tool installed and is not run here.

Article author: Arthur Isaev