Parent Keyword: การเข้าถึง Methods ของ Class แม่
ในบทเรียนที่แล้ว เราได้เรียนรู้เกี่ยวกับการ Overriding Methods ซึ่ง Class ลูกสามารถสร้าง Method ที่มีชื่อและ Parameter เหมือนกับ Method ใน Class แม่ แต่มีการเปลี่ยนแปลงการทำงานภายใน Method นั้นเอง ในบทนี้ เราจะมาเรียนรู้เกี่ยวกับการใช้ parent keyword ซึ่งเป็นเครื่องมือสำคัญที่จะช่วยให้เราสามารถเข้าถึง Methods ของ Class แม่ได้จากภายใน Class ลูก
ความหมายของ Parent Keyword
parent keyword ใน PHP ถูกใช้เพื่ออ้างอิงถึง Class แม่ (Parent Class หรือ Super Class) ของ Class ที่กำลังใช้งานอยู่ โดยเฉพาะอย่างยิ่งเมื่อเราต้องการเข้าถึง Properties หรือ Methods ที่ถูก define ไว้ใน Class แม่จากภายใน Class ลูก
การใช้งาน Parent Keyword
การใช้งาน parent keyword จะมาพร้อมกับ operator :: (Scope Resolution Operator) เพื่อระบุว่าเราต้องการเข้าถึงอะไรจาก Class แม่ รูปแบบการใช้งานทั่วไปคือ:
parent::methodName(); // เรียกใช้งาน method จาก Class แม่
parent::$propertyName; // เข้าถึง property จาก Class แม่ (ไม่ค่อยพบบ่อย)
ลองมาดูตัวอย่างการใช้งาน parent keyword ในสถานการณ์ที่เราทำการ Override Method:
<?php
class Animal {
public function makeSound() {
echo "Generic animal sound\n";
}
}
class Dog extends Animal {
public function makeSound() {
echo "Woof!\n";
}
public function displayParentSound() {
echo "Parent says: ";
parent::makeSound(); // เรียก makeSound() ของ Class Animal
}
}
$myDog = new Dog();
$myDog->makeSound(); // Output: Woof!
$myDog->displayParentSound(); // Output: Parent says: Generic animal sound
?>
ในตัวอย่างนี้ Class Dog สืบทอดมาจาก Class Animal และทำการ Override Method makeSound() นอกจากนี้ยังมี Method displayParentSound() ที่ใช้ parent::makeSound() เพื่อเรียก Method makeSound() ของ Class Animal ซึ่งแสดงให้เห็นว่าเราสามารถเข้าถึง Method ของ Class แม่ได้ แม้ว่า Class ลูกจะทำการ Override Method นั้นไปแล้วก็ตาม
ตัวอย่างเพิ่มเติม: Constructor และ Parent Keyword
parent keyword มักถูกใช้ใน Constructor ของ Class ลูก เพื่อเรียก Constructor ของ Class แม่ เพื่อให้แน่ใจว่า Properties ที่จำเป็นใน Class แม่ได้รับการ Initialize อย่างถูกต้อง:
<?php
class Vehicle {
protected $brand;
public function __construct($brand) {
$this->brand = $brand;
echo "Vehicle constructor called.\n";
}
public function getBrand() {
return $this->brand;
}
}
class Car extends Vehicle {
private $model;
public function __construct($brand, $model) {
parent::__construct($brand); // เรียก constructor ของ Class Vehicle
$this->model = $model;
echo "Car constructor called.\n";
}
public function getModel() {
return $this->model;
}
}
$myCar = new Car("Toyota", "Camry");
echo "Brand: " . $myCar->getBrand() . "\n"; // Output: Brand: Toyota
echo "Model: " . $myCar->getModel() . "\n"; // Output: Model: Camry
?>
ในตัวอย่างนี้ Class Car สืบทอดมาจาก Class Vehicle Constructor ของ Class Car เรียก parent::__construct($brand) เพื่อเรียก Constructor ของ Class Vehicle และทำการ Initialize Property $brand จากนั้น Constructor ของ Class Car จะทำการ Initialize Property $model ของตัวเอง
ข้อดีของการใช้ Parent Keyword
- Code Reuse: ช่วยให้เราสามารถ Reuse Code จาก Class แม่ได้ โดยไม่ต้องเขียน Code ซ้ำ
- Maintainability: ทำให้ Code ง่ายต่อการบำรุงรักษา เพราะหากมีการแก้ไข Code ใน Class แม่ Class ลูกก็จะได้รับผลกระทบโดยอัตโนมัติ (ในกรณีที่ไม่ได้ Override)
- Extensibility: ช่วยให้เราสามารถขยายความสามารถของ Class แม่ได้ โดยการ Override Methods และเรียกใช้ Methods เดิมจาก Class แม่ด้วย
parentkeyword - Clarity: ทำให้ Code อ่านง่ายและเข้าใจได้ง่ายขึ้น เพราะบ่งบอกอย่างชัดเจนว่าเรากำลังเรียกใช้ Method ของ Class แม่
ข้อควรระวัง
แม้ว่า parent keyword จะมีประโยชน์มาก แต่ก็มีข้อควรระวังบางประการ:
- ความสัมพันธ์แบบ Inheritance:
parentkeyword สามารถใช้ได้เฉพาะใน Class ที่สืบทอดมาจาก Class อื่นเท่านั้น หากใช้ใน Class ที่ไม่ได้สืบทอด จะเกิด Error - Visibility:
parentkeyword สามารถเข้าถึง Properties และ Methods ที่มี Access Modifier เป็นpublicหรือprotectedเท่านั้น ไม่สามารถเข้าถึง Properties และ Methods ที่มี Access Modifier เป็นprivateได้ - ลำดับการเรียก Constructor: เมื่อเรียก Constructor ของ Class แม่ด้วย
parent::__construct()ต้องแน่ใจว่าได้เรียกก่อนที่จะทำการ Initialize Properties อื่นๆ ใน Class ลูก เพื่อป้องกัน Error ที่อาจเกิดขึ้น
สรุป
parent keyword เป็นเครื่องมือที่สำคัญในการเขียนโปรแกรมเชิงวัตถุใน PHP ช่วยให้เราสามารถเข้าถึง Properties และ Methods ของ Class แม่ได้จากภายใน Class ลูก ทำให้ Code ของเรามีความยืดหยุ่น ง่ายต่อการบำรุงรักษา และสามารถ Reuse Code ได้อย่างมีประสิทธิภาพ ในบทถัดไป เราจะมาเรียนรู้เกี่ยวกับ Interfaces ซึ่งเป็นอีกหนึ่งแนวคิดที่สำคัญในการออกแบบ Class และการทำ Polymorphism