Skip to content

PlusMagi's Blog By Pitt Phunsanit

Plus emotional magic to the knowledge of logic.

  • About’s Pitt
Close Button
PlusMagi's Blog By Pitt Phunsanit PHP PHP: PER

PHP: PER

2022-04-082022-04-08| phunsanitphunsanit| 0 Comment | 07:00
Categories:
  • PHP

วงการ PHP ในยุคนี้ไม่ได้มีแค่ PSR (PHP Standard Recommendations) จากกลุ่ม FIG อีกต่อไปแล้วครับ เพราะตอนนี้เรามี PER (PHP Ecosystem Readiness) ซึ่งเป็นมาตรฐานใหม่ที่กำลังเข้ามามีบทบาทสำคัญมากในการเขียนโค้ด PHP ร่วมสมัย

บทความนี้จะพาคุณไปทำความรู้จักว่า PER คืออะไร ทำไมมันถึงเกิดขึ้นมา และมันจะเปลี่ยนวิธีเขียนโค้ดของคุณไปอย่างไรครับ


ทำความรู้จักกับ PER (PHP Ecosystem Readiness)

PER หรือ PHP Ecosystem Readiness คือมาตรฐานการเขียนโค้ด (Coding Style Standards) ชุดใหม่ที่พัฒนาต่อยอดมาจาก PSR โดยกลุ่ม PHP-FIG (PHP Framework Interoperability Group)

ถ้าจะอธิบายให้เห็นภาพที่สุด

PSR-12 คือมาตรฐานที่เราใช้กันมานาน แต่เนื่องจากภาษา PHP มีการอัปเดตฟีเจอร์ใหม่ ๆ อย่างรวดเร็วในเวอร์ชัน 8.x (เช่น Attributes, Constructor Property Promotion, Match Expressions) ทำให้ PSR-12 เริ่มตามไม่ทัน กลุ่ม FIG จึงตัดสินใจสร้าง PER Coding Style ขึ้นมาเพื่อเป็นมาตรฐานที่ “มีชีวิต” (Living Standard) ที่จะอัปเดตตัวเองโดยอัตโนมัติเมื่อมี PHP เวอร์ชันใหม่ออกมา โดยไม่ต้องคอยประกาศเป็น PSR เลขใหม่เรื่อย ๆ


ความเปลี่ยนแปลงจาก PSR-12 สู่ PER

จุดประสงค์หลักของ PER คือการรองรับ Syntax ใหม่ๆ ของ PHP 8.x เป็นต้นไป โดยสิ่งที่คุณจะได้เห็นและต้องปรับตัวตามมาตรฐาน PER มีดังนี้ครับ


Constructor Property Promotion

ฟีเจอร์เด่นของ PHP 8.x ที่ช่วยลดรูปการประกาศ Property ใน Constructor สำหรับ PER จะกำหนดให้จัดฟอร์แมตแบบแยกบรรทัดเพื่อความอ่านง่าย

// มาตรฐาน PER Coding Style
class Point 
{
    public function __construct(
        protected int $x,
        protected int $y,
        protected int $z = 0,
    ) {}
}

Match Expressions

โครงสร้าง match ที่มาแทน switch ในหลายๆ กรณี PER กำหนดให้เขียนโดยมีเว้นวรรคและตัดขึ้นบรรทัดใหม่ให้ชัดเจน รวมถึงใส่ Comma ตัวสุดท้าย (Trailing Comma) ด้วย

$result = match ($operator) {
    '+' => $x + $y,
    '-' => $x - $y,
    default => throw new InvalidArgumentException('Not supported'),
};

Union Types และ Intersection Types

การระบุ Type แบบหลายตัวเลือก (เช่น string|int) PER กำหนดว่าห้ามมีช่องว่างระหว่างเครื่องหมาย | หรือ &

// ถูกต้องตาม PER
public function processData(string|int $input): void 
{
    // ...
}

Attributes

การใช้ Attributes (Metadata) จะต้องประกาศไว้ที่บรรทัดเหนือสิ่งที่เราต้องการจะอ้างอิง และหากมีหลายตัว แนะนำให้แยกบรรทัดกัน

#[AsController]
#[Route('/api/v1/users')]
class UserController
{
    // ...
}

ทำไมเราถึงควรเปลี่ยนมาใช้ PER?

  • ไม่เอ้าท์ (Future-Proof): เนื่องจากเป็น Living Standard เมื่อ PHP 8.4, 8.5 หรือ 9.0 ออกมา มาตรฐาน PER ก็จะอัปเดตเกณฑ์ตามทันที โดยที่เราไม่ต้องรอเปลี่ยนไปใช้ PSR-13 หรือ PSR-14
  • รองรับเครื่องมืออัตโนมัติ: เครื่องมือตรวจโค้ด (Linters/Formatters) ยอดนิยมอย่าง PHP_CodeSniffer หรือ PHP-CS-Fixer ได้เริ่มเปลี่ยนมาใช้ PER เป็นค่าเริ่มต้น (Default) หรือมี Rule ให้เลือกใช้แล้ว
  • โค้ดสะอาดและเป็นหนึ่งเดียว: ช่วยให้ทีมพัฒนาที่ใช้ PHP 8.x ขึ้นไป มีแนวทางการตัดบรรทัด การเว้นวรรคที่เป็นระบบเดียวกัน ลดปัญหา Conflict เวลาตรวจโค้ด (Code Review)

วิธีเริ่มต้นใช้งาน PER ในโปรเจกต์ของคุณ

หากคุณใช้งาน PHP-CS-Fixer (เครื่องมือจัดฟอร์แมตโค้ดอัตโนมัติ) คุณสามารถเริ่มใช้ PER ได้ง่ายๆ โดยการตั้งค่าในไฟล์ .php-cs-fixer.dist.php ดังนี้

<?php

$finder = (new PhpCsFixer\Finder())
    ->in(__DIR__)
    ->exclude('var')
;

return (new PhpCsFixer\Config())
    ->setRules([
        '@PER-CS' => true, // เปิดใช้งานมาตรฐาน PER Coding Style ล่าสุด
        // คุณสามารถใส่กฎอื่นๆ เพิ่มเติมตรงนี้ได้
    ])
    ->setFinder($finder)
;

สรุป

PER (PHP Ecosystem Readiness) ไม่ใช่การล้มล้าง PSR แต่คือการ “อัปเกรด” ให้มาตรฐานการเขียนโค้ด PHP ก้าวทันความทันสมัยของตัวภาษาในปัจจุบัน หากคุณกำลังเริ่มโปรเจกต์ใหม่ หรือกำลังดูแลโปรเจกต์ที่เป็น PHP 8.x แนะนำให้ขยับจาก PSR-12 มาใช้ @PER-CS ได้เลยครับ เพื่อให้โค้ดของทีมคุณอ่านง่าย ทันสมัย และพร้อมรองรับอนาคตอย่างแท้จริง


อ่านเพิ่มเติม

  • PHP: Coding Standards Fixer (PHP CS Fixer )
  • จัด code php ตาม PHR 2
  • จัด code php ตาม PHR 2 สำหรับคนไม่ใช้ PHP7
ป้ายกำกับ:@PER-CS, Attributes, Code Review, Coding Style Standards, Conflict, Constructor, Ecosystem, FIG, Formatters, Intersection Types, Linters, Match Expressions, Metadata, PER, PHP, PHP Ecosystem Readiness, PHP Framework Interoperability Group, PHP Standard Recommendations, PHP_CodeSniffer, PHP-CS-Fixer, PHP-FIG, Promotion, Property, PSR, PSR-12, PSR-13, PSR-14, Readiness, Rule, Switch, Trailing Comma, Union Types, ตรวจโค้ด, ฟอร์แมต, ฟีเจอร์, มาตรฐาน, ลดปัญหา, เครื่องหมาย

แนะแนวเรื่อง

PREVIOUS Previous post: Linux: chmod สร้างสิทธิ์ (Permissions )
NEXT Next post: PHP: mod_php: อดีตตัวแรง ผู้อุ้มชูโลก PHP

Projects

  • Statement Columns Mapping Helper
  • PlusMagi Site Search
  • jQuery Plus Repeater

Recent Posts

  • AI:thClaws
  • Laravel: php artisan tinker
  • Line: API มันทำอะไรได้บ้าง
  • Laravel: reset password
  • AWS S3 คืออะไร ? คู่มือครบจบสำหรับผู้เริ่มต้น

Archives

Categories

  • AI (10)
  • Businesses (4)
  • Design (36)
    • UX/UI (6)
  • DevOps (55)
    • CI/CD (2)
    • Docker (29)
    • GIT (25)
  • Histories (10)
  • Life (72)
    • Books (27)
    • Tips and Tricks (13)
    • พุทธ (3)
  • Network (114)
    • Apache HTTP Server (13)
    • IOT (1)
    • Nginx (27)
    • Stalwart (5)
  • Operating Systems (242)
    • Unix-like (171)
      • Android (13)
      • iPhone (5)
      • Linux (107)
      • macOS (84)
        • Homebrew (15)
        • OrbStack (10)
      • Oh My ZSH (4)
      • Shell Script (32)
      • SSH (11)
    • Windows (111)
      • PowerShell (23)
      • WSL (24)
  • Programming (646)
    • .NET (18)
      • .NET Core EF (5)
      • C# (17)
    • API (21)
      • REST (5)
      • Swagger (6)
    • Database (173)
      • DBeaver (3)
      • MariaDB (26)
      • MySql (64)
      • Oracle Database (5)
        • 10g (2)
      • PostgreSQL (6)
      • RDBMS (1)
      • SQL Server (84)
        • SSMS (8)
        • T-SQL (26)
      • SQLite (1)
    • PowerBuilder (10)
    • Python (1)
    • Rust (1)
    • System Analyst (SA) (1)
    • Testing (11)
      • Automated Testing (3)
    • UML (2)
    • Web (339)
      • Backend (218)
        • Golang (1)
        • Java (44)
          • Spring Boot (14)
        • Node.js (1)
        • PHP (173)
          • Laravel (16)
          • Yii (5)
      • Frontend (120)
        • CSS (15)
          • Tailwind CSS (3)
        • JavaScript (111)
          • Angular (2)
          • jQuery (60)
          • Tabulator (11)
          • Vue.js (3)
      • WordPress (25)
  • Programs (72)
    • Excel (9)
  • Security (46)
  • กฎหมาย (6)

Sirat WordPress Theme By VWThemes

Scroll Up
Go to mobile version