This here are the basic naming conventions which are followed by most big tech companies that uses PHP.
This naming conventions is in compliance with Laravel [PHP] coding Standards with PSR specifications.
Best combined with SOLID Principles
And remember to follow the KISS Principle whenever possible
Listed here are the naming standards:
- For Classes, Interfaces/Contracts, Traits: use
PascalCase
- For Constants: use
TITLE_CASE
- For Functions/Methods, Class Properties, Variables: use
camelCase
- For Array Indices/Database Field Names/Model Fillables/Model Relations: use
lower_snake_case
- For Routes: use
lower-kebab-case
Below are the standards use cases and samples :
For Classes, Interfaces/Contracts, Traits: usePascalCase
Class:
class AuthController extends Controller
{
//
}
Interface/Contracts
interface LoginInterface
{
/**
}
Traits
trait Audit
{
/**
}
For Constants: use TITLE_CASE
namespace App\Constants;
class AppConstant {
const DEFAULT_PAGE = 1;
const DEFAULT_PAGE_LIMIT = 10;
const MAX_PAGE_LIMIT = 100;
const ALPHANUMERIC_CHARACTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
const NUMERIC_CHARACTERS = '0123456789';
const ALPHA_CHARACTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
const UPPERCASE_ALPHA_CHARACTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
const LOWERCASE_ALPHA_CHARACTERS = 'abcdefghijklmnopqrstuvwxyz';
}
For Functions/Methods, Class Properties, Variables: use camelCase
Functions/Methods
public function refreshToken() : JsonResponse {
return $this->loginService->refreshToken();
}
Class Properties
class AuthController extends Controller
{
// these are the class properties
protected $loginService;
protected $logoutService;
}
Variables
public function __construct(LoginService $loginService, LogoutService $logoutService) {
$this->loginService = $loginService;
$this->logoutService = $logoutService;
}
For Array Indices/Database Field Names/Model Fillables/Model Relations: use lower_snake_case
Array Indices
foreach($age as $x => $x_value) {
return $x_value;
}
Database Field Names
public function up()
{
Schema::create('audits', function (Blueprint $table) {
$table->id();
$table->string('user_type')->nullable();
$table->unsignedBigInteger('user_id')->nullable();
$table->index(['user_id', 'user_type']);
});
}
Model Fillables
protected $fillable = [
'first_name',
'last_name',
'username',
'email',
'password',
];
For Routes: use lower-kebab-case
Route::group(['middleware' => 'auth:api'], function() {
Route::post('refresh-token', [AuthController::class, 'refreshToken']);
Route::post('logout', [AuthController::class, 'logout']);
});
Top comments (1)
Model fillables needs to match with database columns so that would work fine but any Class that is connected to database like Eloquent model classes, the class properties (camelCase) also needs to match with database columns(lower_snake_case).