Every developer should follow the PHP Standards Recommendations
Expand |
---|
title | 1. Naming conventions |
---|
|
1.1 Variables: - Use camelCase for variable names.
- Variable names should be descriptive and concise.
- Avoid abbreviations unless widely recognized.
Code Block |
---|
language | php |
---|
title | Example |
---|
collapse | true |
---|
| $userName = 'John';
$totalAmount = 100; |
1.2 Functions and Methods: - Use camelCase for function and method names.
- Function and method names should clearly express their purpose.
- Methods should start with a verb when possible.
Code Block |
---|
language | php |
---|
title | Example |
---|
collapse | true |
---|
| function calculateTotalAmount($prices)
{
// Logic here
} |
1.3 Classes: - Use PascalCase for class names.
- Class names should be nouns and describe the entity or the functionality.
Code Block |
---|
language | php |
---|
title | Example |
---|
collapse | true |
---|
| class UserController
{
// Class definition
} |
1.4 Constants: - Use UPPER_CASE with underscores (screaming snake case) for constants.
- Constants should be descriptive and represent fixed values.
Code Block |
---|
language | php |
---|
title | Example |
---|
collapse | true |
---|
| define('MAX_RETRY_ATTEMPTS', 5);
const DEFAULT_TIMEOUT = 30; |
1.5 Interfaces: - Use PascalCase with the suffix Interface.
- Interfaces should describe behavior and represent contracts.
Code Block |
---|
language | php |
---|
title | Example |
---|
collapse | true |
---|
| interface CacheInterface
{
public function get(string $key): int;
public function set(string $key, string $value);
} |
1.6 Abstract classes
- Use PascalCase with the prefix Abstract.
Code Block |
---|
language | php |
---|
title | Example |
---|
collapse | true |
---|
| abstract class AbstractJob
{
abstract public function getName(): string;
// Other logic here
} |
1.7 Namespaces: - Use PascalCase for namespaces.
- Namespace names should map to the directory structure.
Code Block |
---|
language | php |
---|
title | Example |
---|
collapse | true |
---|
| namespace App\Controllers; |
|
PSR-12: Extended Coding Style
- Namespaces and classes MUST follow an “autoloading” PSR-0, PSR-4
- Class names MUST be declared in StudlyCaps/PascalCase
- CONSTANTS MUST be declared in all upper case with underscore separators
- Method names MUST be declared in camelCase
- Variable names MUST be written in camelCase and the name describe for what it is (not simply $item, better $termEntryTig)
- UTF-8
- Next file line delimiter: Unix
- Tabs: 4 Spaces
Link to PSR-12 article: https://www.php-fig.org/psr/psr-12/
Principles:
- YAGNI: You ain’t gonna need it (wiki)
- KISS: Keep It Simple, Stupid (wiki)
- DRY: Don’t Repeat Yourself (wiki)
- SOLID: (wiki)
- Single-Responsibility-Prinzip
- Open-Closed-Prinzip
- Liskovsches Substitutionsprinzip
- Interface-Segregation-Prinzip
- Dependency-Inversion-Prinzip