PHP Coding Standards Every Developer Should Follow

Consistent coding standards are the difference between a codebase you enjoy working in and one you dread. They matter even more when you're working remotely with international clients or collaborating across time zones — your code needs to speak clearly without you being present to explain it.

PSR Standards — The Foundation

PHP-FIG (Framework Interop Group) PSR standards are the universal baseline for PHP code. Laravel follows them, and so should you.

PSR-1: Basic Coding Standard

  • Use only opening tags <?php
  • Files must use UTF-8 without BOM
  • Class names in PascalCase
  • Method names in camelCase
  • Constants in UPPER_CASE
// Good class UserService { public function getUserById(int $id): ?User { return User::find($id); } } // Bad class user_service { public function get_user_by_id($id) { return User::find($id); } }

PSR-12: Extended Coding Style

  • 4 spaces for indentation — never tabs
  • Lines should be 120 characters or fewer
  • One blank line between methods
  • Type declarations on all method signatures
// Good if ($condition) { $result = processData($data); return $result; } // Bad if($condition){ $result=processData($data);return $result; }

Laravel-Specific Conventions

Naming Conventions

// Controllers — PascalCase, singular, "Controller" suffix class UserController extends Controller {} // Models — singular PascalCase class BlogPost extends Model {} // Variables — camelCase $userData = []; $isActive = true; // Database tables — plural snake_case // users, blog_posts, user_profiles // Routes — kebab-case Route::get('/user-profiles', ...); Route::get('/blog-posts/{post}', ...);

Always Type-Hint Your Methods

// Good — clear, self-documenting, IDE-friendly public function createUser(array $data): User { return User::create($data); } // Bad — no one knows what goes in or comes out public function createUser($data) { return User::create($data); }

Document Non-Obvious Methods

/** * Calculate pro-rated salary for partial month employment. * * @param User $user * @param Carbon $startDate First day of employment in the period * @param int $workingDays Total working days in the month * @return float Gross salary amount in BDT */ public function calculateProratedSalary(User $user, Carbon $startDate, int $workingDays): float { // ... }

Automated Quality Tools

PHP CS Fixer — Auto-format your code

composer require --dev friendsofphp/php-cs-fixer # Fix all files in app/ ./vendor/bin/php-cs-fixer fix app/

PHP CodeSniffer — Check standards compliance

composer require --dev squizlabs/php_codesniffer # Check ./vendor/bin/phpcs --standard=PSR12 app/ # Auto-fix where possible ./vendor/bin/phpcbf --standard=PSR12 app/

Team Practices

  • Add a .editorconfig to your repo — enforces consistent indentation and line endings across all editors automatically
  • Set up a Git pre-commit hook to run PHP CS Fixer before every commit
  • Write code and comments in English — it's universal and expected by international clients and open source contributors
  • Run code reviews on every PR — the act of explaining your code to a colleague is itself a quality filter

Conclusion

Start with PSR-12 and Laravel's own conventions — they cover 90% of the decisions you'll face. Add PHP CS Fixer to your workflow to automate formatting entirely so you never have to think about it again. The goal isn't perfect code, it's consistent code that any developer can read and modify confidently.

About the Author

Kamruzzaman Polash — Software Engineer specialising in Laravel, REST APIs, and scalable backend systems. 10+ projects delivered for clients worldwide.