PHP Standards and Coding Styles Followed by EF Laravel Developers
PHP Standards and Coding Styles are important guidelines and conventions that developers follow to write clean, readable, and maintainable PHP code. Adhering to coding standards helps ensure consistency and collaboration among developers working on a project. There are several popular PHP coding standards and style guides.
In this article, I am going to share the coding standards followed by Electronic First Laravel developers.
Table of Contents
Comment
<?php
// This is a commentSpaces
Spaces: 4
class MyController extends Controller
{
public function index()
{
$title = "The Title";
return view('example', compact('title'));
}
}App Folder
Folder name: First letter Capital
App\Helper
App\ServicesFile name: StudlyCaps
MyService.php
TheExampleJob.phpViews
Folder name: Lowercase and hyphen between two words.
components
the-componentsFile name: Lowercase and hyphen between two words.
components.blade.php
the-components.blade.phpClass
StudlyCaps (also known as PascalCase)
class MyClass
{
//
}Properties
camelCase
$number = 10;
$secondNumber = 20;Files
File name (img, js, css etc.): Lowercase and hyphen between two words.
filename-timestamp.jpg
my-style.cssLines
Add a maximum of 1 line gap before: return view()
public function index()
{
$title = "The Title"; // One line after $title
return view('example', compact('title'));
}Add a maximum of 1 line gap between two functions.
public function fun1()
{
//
}
public function fun2()
{
//
}Function
Function comment with arguments and return types:
/**
* This is a comment
* @param Request $request
* @return \Symfony\Component\HttpFoundation\BinaryFileResponse
*/
public function index(Request $request)
{
//
}Todo
/**
* TODO: Needs improvement here
*/
public function index(Request $request)
{
//
}Error Handling
Try catch:
public function generateToken()
{
try {
// API call
} catch (\Exception $ex) {
Log::error('Indentifier', [
'class' => __CLASS__,
'function' => __FUNCTION__,
'message' => $ex->getMessage(),
]);
return response()->json([
'success' => false,
'message' => $ex->getMessage()
]);
}
}Cache Name
$cacheName = cache("product:{$id}");
$cacheName = cache("productTranslate:{$id}");Database
DB Transaction: multiple tables, long task
DB::beginTransaction();
try {
DB::insert(...);
DB::insert(...);
DB::insert(...);
DB::commit(); // All good
} catch (\Exception $e) {
DB::rollback();
// Log
Log::error('ActionName', [
'class' => __CLASS__,
'function' => __FUNCTION__,
'message' => $ex->getMessage(),
]);
}HTTP Header
Case-insensitive header field names. Studly-Caps
// Custom headers with "X-"
Http::withHeaders([
'Authorization' => $accessToken,
'Content-Type' => 'application/json' ,
'X-Ip-Address' => '127.0.0.1' ,
])->get('https://api');Last updated: 10 June 2024
Md Obydullah
Software Engineer | Ethical Hacker & Cybersecurity...
Md Obydullah is a software engineer and full stack developer specialist at Laravel, Django, Vue.js, Node.js, Android, Linux Server, and Ethichal Hacking.