Coding Standards
Naming Conventions
Variables
Use descriptive, meaningful variable names in camelCase. Avoid single-letter or ambiguous names.
// Good
$userName = 'John Doe';
$invoiceTotal = 1500;
// Bad
$u = 'John Doe';
$x = 1500;
Methods
Method names should be in camelCase and clearly describe their purpose. Use verbs for actions.
// Good
public function getUserName()
{
return 'John Doe';
}
public function calculateInvoiceTotal($items)
{
// ...
}
// Bad
public function getuser() {
return 'John Doe';
}
public function calc() {
// ...
}
Classes
Class names should use PascalCase and be singular, representing a single entity or concept.
// Good
class UserManager
{
// class content
}
class Invoice
{
// ...
}
// Bad
class user_manager
{
// class content
}
class invoices
{
// ...
}
Constants
Constants should be in UPPER_CASE with underscores. Use them for values that do not change.
// Good
const MAX_USER_COUNT = 100;
const DEFAULT_TIMEOUT = 30;
// Bad
const maxUserCount = 100;
const defaultTimeout = 30;
Formatting
Indentation
Use 4 spaces per indentation level. Do not use tabs. Indent consistently for readability.
// Good
if ($condition) {
echo 'true';
}
// Bad
if ($condition) { echo 'true'; }
Line Length
Keep lines under 80 characters. Break long statements into multiple lines.
// Good
$longString = 'This is a long string that is split across multiple lines to keep each line under 80 characters.';
Blank Lines
Use blank lines to separate logical sections of code, such as between functions or class methods.
// Good
public function firstFunction()
{
// function body
}
public function secondFunction()
{
// function body
}
// Bad
public function firstFunction()
{
// function body
}
public function secondFunction()
{
// function body
}
Comments
Use comments to explain non-obvious code. Write comments above the code they describe, not inline. Avoid redundant comments.
// Good
// This function returns the user's name
public function getUserName(): string
{
return 'John Doe';
}
// Explain complex logic
// Calculate the total invoice amount including tax
public function calculateInvoiceTotal($items)
{
// ...
}
// Bad
public function getUserName(): string
{
return 'John Doe'; // returns name
}
Additional Best Practices
- Use type hints and return types in functions and methods for clarity and error prevention.
- Group related functions and classes together in files and folders.
- Avoid magic numbers and strings; use named constants instead.
- Use PSR-12 coding style as a baseline for formatting and structure.
- Refactor duplicated code into reusable functions or classes.
- Write self-documenting code: code should be clear enough that comments are only needed for complex logic.
Example: Comprehensive Class
/**
* Manages user operations such as registration and authentication.
*/
class UserManager
{
/**
* Registers a new user.
*
* @param array $data
* @return User
*/
public function register(array $data): User
{
// ... registration logic ...
}
/**
* Authenticates a user.
*
* @param string $email
* @param string $password
* @return bool
*/
public function authenticate(string $email, string $password): bool
{
// ... authentication logic ...
}
}