Last Update: 22 Oct 2024
In the world of PHP development, adhering to coding standards is essential for creating maintainable and readable code. Standards like PSR-1, PSR-12, and PSR-4 provide valuable guidelines that help developers write better PHP code. Many of these rules can be flexible. Not following them won’t cause immediate errors. However, it’s important to know that ignoring them can affect code quality over time. This blog shares important rules for writing PHP code. These rules are not strictly required but are best practices. Following them can help improve readability and maintainability. We’ll accompany each rule with practical examples to illustrate their importance.
PSR-1 outlines essential guidelines for PHP coding, serving as the foundation for writing clear and consistent code. In this section, we’ll explore some specific rules within PSR-1 that are particularly relevant to our discussion. These rules are not strictly required, but they are good practices. They can improve the quality and maintainability of your PHP code.
1. Opening Tags:
<?php
or <?=
for opening PHP tags.<?
may work if short_open_tag
is enabled, but they’re not recommended.
// Best practice
<?php
echo "Hello, World!";
// Still works if short_open_tag is enabled (not recommended)
<?
echo "Hello, World!";
2. Class Naming:
PascalCaps
for class names.myclass
will function correctly but lacks conventional clarity
// Best practice
class MyClass
{
// ...
}
// Still valid (not recommended)
class myclass
{
// ...
}
3. Method Naming:
camelCase
for method names.my_method
will still work but strays from the standard.
// Best practice
public function myMethod()
{
// ...
}
// Still valid (not recommended)
public function my_method()
{
// ...
}
As we delve into PSR-12, we find more detailed guidelines that promote consistency. Here are some rules that can be bent without breaking the code:
1. Function and Class Braces:
// Best practice
class MyClass
{
public function myMethod()
{
// ...
}
}
// Still valid (not recommended)
class MyClass {
public function myMethod() {
// ...
}
}
2. Function Arguments:
// Best practice
function functionName(int $arg1, string $arg2): string
{
return "Result";
}
// Valid but not recommended
function functionName($arg1, $arg2)
{
return "Result";
}
// Best practice
function functionName(int $arg1, string $arg2): string
{
return "Result";
}
// Still valid (not recommended)
function functionName(int $arg1, string $arg2):string
{
return "Result";
}
3. Control Structures:
// Best practice
for ($i = 0; $i < 10; $i++) {
// ...
}
// Still valid (not recommended)
for($i=0;$i<10;$i++) {
// ...
}
4. Type Casting:
(int)a
without spaces, and it will still work
// Best practice
$value = (int) $a;
// Still valid (not recommended)
$value = (int)a;
5. Operators:
// Best practice
$result = $a + $b;
// Valid but not recommended
$result=$a+$b;
PSR-4 provides a way to autoload classes based on their namespace. While many rules here are more stringent, there are still flexible points to consider:
Namespace Declaration:
// Properly structured
namespace App\Models;
class User { }
// Still works (manual inclusion required)
namespace MyApp;
include 'User.php'; // Manual inclusion is necessary
Don’t worry, we don’t spam!