Skip to main content

แนะนำคุณสมบัติใหม่ ๆ ใน PHP 7.4 - 8.0 เฉพาะตัวที่ชอบ

Kongvut Sangkla
Tags:

Intro

สวัสดีครับ นาน ๆ จะมีเขียนบทความภาษา PHP (น่าจะเป็นบทความแรก) เพราะมองว่าน่าจะมีประโยชน์ โดยจะแนะนำคุณสมบัติใหม่ ๆ ที่ส่วนตัวใช้บ่อยแล้วรู้สึกชอบตั้งแต่ PHP 7.4 - 8.0 โดยจะไม่ได้อธิบาย Features ใหม่ทั้งหมด แต่จะเลือกเอามาอธิบายเฉพาะที่เคยใช้งานแล้วชอบ มีความสะดวกเพิ่มมากขึ้นมาเล่าให้ฟัง 😊

Arrow functions (PHP 7.4+)

และแล้ว PHP ก็มี Arrow functions ให้ใช้คล้ายของ JavaScript แล้วโดยข้อดีก็คือ

  • ดู Clean Code เรียบง่าย และสั้นลง
  • เป็นแบบ One-liner functions
  • หากต้องการเข้าถึงตัวแปรภายนอก Scope ได้เลย ไม่ต้องใช้ use() เหมือน Callback functions เหมือนรุ่นก่อน ๆ แล้ว
Arrow functions (ตัวอย่างที่ 1)
$y = 1;
// แบบเดิม
$fn1 = function ($x) use ($y) {
return $x + $y;
};
// แบบใหม่
$fn2 = fn($x) => $x + $y;

// ผลลัพธ์ = 3
echo $fn1(2);
echo $fn2(2);
Arrow functions (ตัวอย่างที่ 2)
$z = 1;
// แบบเดิม
$fn1 = function ($x) use ($z) {
return function ($y) use ($x, $z) {
return $x * $y + $z;
};
};
// แบบใหม่
$fn2 = fn($x) => fn($y) => $x * $y + $z;

// ผลลัพธ์ = 31
echo $fn1(3)(10);
echo $fn2(3)(10);

Null-safe operator (PHP 8.0+)

การดำเนินการตัวนี้คล้าย ๆ กับ Optional Chaining ของ JavaScript โดย Null-safe operator คือการใส่สัญลักษณ์ ?-> เพื่อให้ Return Null โดยจะไม่ได้ Error ใด ๆ เมื่อไม่มี Property หรือ Method นั้นอยู่

Null-safe operator
class Customer {
public function getAddress() {}
}

$country = new Customer();
$country = $customer->getAddress()->getCountry();
Error messages

Fatal error: Uncaught Error: Call to a member function getCountry() on null in ...:...

จะเห็นว่าจะได้รับ Error เมื่อเรียก Method getCountry() (เพราะไม่ได้มีการประกาศไว้)

Null-safe operator
// การแก้ปัญหาแบบเดิมทำได้ 2 วิธีแบบนี้

// แบบเดิมวิธีที่ 1
$address = $customer->getAddress();
if ($address) {
$country = $address->getCountry();
}
else {
$country = null;
}

// แบบเดิมวิธีที่ 2
$address = $customer->getAddress();
$country = $address ? $address->getCountry() : null;

// แบบใหม่ที่ใช้ Null-safe operator เติม (?->) เข้าไปก่อนหน้า Method ที่ต้องการทำ Null-safe
$country = $customer->getAddress()?->getCountry();
note

รายละเอียดเพิ่มเติมเกี่ยวกับ Null-safe operator https://php.watch/versions/8.0/null-safe-operator

Null coalescing assignment operator (PHP 7.4+)

ตัวนี้ถือเป็น Operator ที่ผมใช้บ่อยมาก ๆ และมีประโยชน์มากเพราะทำให้ Code ดูสั้นลงมาก โดยประโยชน์ของ Null coalescing assignment operator คือ การกำหนดค่าให้ตัวแปร เมื่อไม่มี Property นั้นอยู่

Null coalescing assignment operator
$parameters = [
'a' => 'Apple'
];

// แบบเดิม 1
if (!isset($parameters['b'])) {
$parameters['b'] = 'default';
}

// แบบเดิม 2
$parameters['b'] = isset($parameters['b']) ? $parameters['b'] : 'default';
// หรือ
$parameters['b'] = isset($parameters['b']) ?: 'default';

// เมื่อใช้ Null coalescing assignment operator จะสามารถกำหนดค่าได้แบบนี้
$parameters['b'] = $parameters['b'] ?? 'default';

Array spread operator (PHP 7.4+)

Spread operator คือ การดำเนินแยกค่าใน Arrays ออกเป็น Values โดย Operator นี้จะคล้าย ๆ ในภาษา JavaScript

Array spread operator
$arrayA = [1, 2, 3];

$arrayB = [4, 5];

// ตัวอย่างที่ 1
$result = [0, ...$arrayA, ...$arrayB, 6 ,7];
// ผลลัพธ์ของ $result คือ [0, 1, 2, 3, 4, 5, 6, 7]

// ตัวอย่างที่ 2
function test(...$result) {
print_r($result);
}

test($arrayA, $arrayB);
// ผลลัพธ์คือ
/*Array
(
[0] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
[1] => Array
(
[0] => 4
[1] => 5
)
)*/

Spaceship operator (PHP 7.0+)

ตัวนี้แถมครับพอดีผ่านไปเจอ ผมเองก็เพิ่งเคยเห็น และเพิ่งรู้เหมือนกันว่ามี Operators นี้มาตั้งแต่รุ่น 7.0

ตัวดำเนินการยานอวกาศ (เป็นชื่อที่แปลกดี) 😅 โดยจะ Return ผลลัพธ์ 3 ค่าดังนี้

  • 0 เมื่อทั้งสองฝั่งมีค่าเท่ากัน
  • 1 เมื่อฝั่งซ้ายมีค่ามากกว่า
  • -1 เมื่อฝั่งขวามีค่ามากกว่า
Spaceship operator
1 <=> 2; // ให้ผลลัพธ์ -1 เพราะ 2 มีค่ามากกว่า 1

// ความสามารถในการเปรียบเทียบตัวอักษร
'a' <=> 'z'; // -1
'Test' <=> 'test'; // -1 (เนื่องจากตัวพิมพ์เล็กมาหลังตัวพิมพ์ใหญ่ในตาราง ASCII จึงมีค่ามากกว่า)
'test' <=> 'test'; // 0
'a1' <=> 'a1'; // 0
'a1' <=> 'a0'; // 1
'a' <=> 'z'; // -1
'Z' <=> 'z'; // -1

// ความสามารถในการเปรียบเทียบอาเรย์
[2, 1] <=> [2, 1]; // 0

// ความสามารถในการเปรียบเทียบ Nested arrays
[[1, 2], [2, 2]] <=> [[1, 2], [1, 2]]; // 1

References:

Loading...