PSR-12 Extended Coding Style
มาตรฐานการจัดรูปแบบโค้ด PHP ที่ทุกคนควรใช้ — แทนที่ PSR-2 ที่ deprecated แล้ว
ทำไมต้อง PSR-12?
1. พื้นฐานที่ต้องรู้ก่อน
<?php
namespace App\Services;
use App\Contracts\PaymentGateway;
use App\Models\Order;
class PaymentService
{
// โค้ดของเรา...
}
// ← บรรทัดว่างท้ายไฟล์2. การประกาศ Namespace และ Use
3. Class, Property และ Method
3.1 การประกาศ Class
3.2 ถ้า implements เยอะมาก
4. Control Structures (if, for, switch)
4.1 If / Elseif / Else
4.2 Switch
4.3 For / Foreach / While
5. Method และ Function Parameters
5.1 Parameter สั้นๆ
5.2 Parameter ยาวๆ (เกิน 120 ตัวอักษร)
6. Closure (Anonymous Function)
Arrow Function (PHP 7.4+)
7. Return Type Declaration
8. Operators
9. Nullable Type และ Union Type
10. ตั้งค่าให้จัดการอัตโนมัติ
PHP CS Fixer (แนะนำ)
Laravel Pint (สำหรับ Laravel)
สรุป
Last updated