PSR-1: Basic Coding Standard
Naming Conventions
Classes:
StudlyCaps
1 2 3
class ClassName { // ... }
Methods:
camelCase
1 2 3 4 5
class ClassName { public function methodName() { // ... } }
Constants:
UPPER_CASE_WITH_UNDERSCORES
1 2 3
class ClassName { const CONSTANT_NAME = 'value'; }
PSR-2: Coding Style Guide
Coding Styling:
- Indentation:
4 spaces
(as the tab) - Opening brances for new class:
on the new line
- Control structure should be followed by:
single space
(if / else / while) - Function structure should:
not have space
(after the function name)
1 2 3 4 5 6 7 8 9 10 11
class ClassName { public function methodName($arg1, $arg2) { if ($arg1 === $arg2) { return true; } else { return false; } } }
PSR-4: Autoloading Standard
PSR-4 is a modern autoloading standard that simplifies the process of including PHP files in your codebase. It requires:
- A one-to-one relationship between class names and file paths.
- Namespace declarations to match the directory structure.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
// ------------------------------------------- // File: src/ExampleNamespace/ExampleClass.php namespace ExampleNamespace; class ExampleClass { // ... } // ------------------------------------------- // Usage of the ExampleClass.php File require 'vendor/autoload.php'; use ExampleNamespace\ExampleClass; $example = new ExampleClass();
Reference: https://reintech.io/blog/php-and-psr-standards-writing-clean-and-compatible-php-code
- Indentation: