If you’ve been developing with PHP for a while, you've likely encountered the term PSR-12. It’s one of the most widely accepted coding standards in the PHP community and is aimed at ensuring consistency in PHP codebases across different projects. Whether you're working solo or as part of a team, following PSR-12 can make your code cleaner, more readable, and easier to maintain. In this blog, we'll break down what PSR-12 is, why it's important, and how you can apply it in your projects.
PSR-12 is a coding style guide for PHP, developed by the PHP-FIG (Framework Interoperability Group). It builds upon the previous PSR-2 standard, providing an updated set of rules that reflect modern PHP practices and improve consistency across codebases.
Think of PSR-12 as the blueprint for writing clean, readable, and maintainable PHP code. By following PSR-12, developers can ensure that their code adheres to a standardized structure, making it easier to collaborate with others and to work on open-source projects.
Coding standards like PSR-12 aren't just about nitpicking over spaces and tabs. Here’s why they matter:
Let’s dive into some of the key rules that PSR-12 lays out. While there are many smaller guidelines, here are the ones that stand out the most.
PSR-12 requires that namespaces and use declarations follow a specific order to improve clarity:
Example:
<?php namespace App\Controllers; use App\Models\User; use App\Repositories\UserRepository; class UserController { // Class implementation }
PSR-12 enforces some strict rules on how classes, properties, and methods should be declared:
class UserController { private $userRepository; public function __construct(UserRepository $userRepository) { $this->userRepository = $userRepository; } } <h4> 4. Control Structures </h4> <p>Control structures like if, for, and switch must follow certain rules:</p> <ul> <li> <strong>Structure</strong>: There must be one space between the control keyword and the opening parenthesis. Curly braces {} must always be used, even for single-line statements.</li> <li> <strong>Indentation</strong>: All blocks inside control structures must be indented by one level (typically four spaces). </li> </ul> <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">if ($user->isAdmin()) { echo "User is an admin"; } else { echo "User is not an admin"; }
Comments are crucial for explaining why certain parts of the code exist. PSR-12 emphasizes the need for proper, clear comments.
<?php namespace App\Controllers; use App\Models\User; use App\Repositories\UserRepository; class UserController { // Class implementation }
To follow PSR-12 in your projects, you can manually adhere to the guidelines when writing code, but the best way to ensure compliance is by using automated tools.
First, get familiar with these general practices:
Manually ensuring that your code follows PSR-12 can be time-consuming, but there are tools that can help you automate this process.
One of the most popular tools for ensuring your PHP code follows PSR-12 is PHP_CodeSniffer. It analyzes your code and points out where you're deviating from the standard.
To install and use it:
class UserController { private $userRepository; public function __construct(UserRepository $userRepository) { $this->userRepository = $userRepository; } }
Then, run it against your code:
if ($user->isAdmin()) { echo "User is an admin"; } else { echo "User is not an admin"; }
Another useful tool is PHP CS Fixer. It not only detects issues but can also fix your code to comply with PSR-12 automatically.
// Fetch user from the repository $user = $userRepository->find($id); /* * If user is not found, throw an exception. * This helps in handling invalid user IDs. */ if (!$user) { throw new NotFoundException(); }
Run the fixer with:
composer require "squizlabs/php_codesniffer=*"
If you use PHPStorm, you can configure it to follow PSR-12. Go to Settings -> Editor -> Code Style -> PHP and set the standard to PSR-12.
Following PSR-12 in your PHP projects is about more than just adhering to arbitrary rules. It’s about making your code cleaner, more readable, and easier to maintain—whether you're working alone or with a team. By following PSR-12, you're not just writing code for yourself but for the entire PHP community.
Adopting this standard is a step towards writing professional, consistent, and maintainable PHP code. Use the tools mentioned to make the process easier, and watch how clean and organized your projects become!
Happy coding!
The above is the detailed content of Understanding PSR- The PHP Coding Style Guide. For more information, please follow other related articles on the PHP Chinese website!