Overriding Methods: การเปลี่ยนแปลงพฤติกรรมใน Class ลูก
ในบทเรียนที่แล้ว เราได้เรียนรู้เกี่ยวกับการสืบทอดคุณสมบัติ (Inheritance) ซึ่งทำให้ Class ลูก (Child Class) สามารถรับ Properties และ Methods จาก Class แม่ (Parent Class) ได้ แต่บางครั้ง เราอาจต้องการให้ Method ที่สืบทอดมานั้นทำงานแตกต่างไปจากเดิมใน Class ลูก นี่คือที่มาของแนวคิดเรื่อง "Overriding Methods"
ความหมายของการ Override
Overriding หมายถึง การที่ Class ลูกสร้าง Method ที่มีชื่อและ Parameter (ชนิดและจำนวน) เหมือนกับ Method ที่มีอยู่ใน Class แม่ แต่มีการเปลี่ยนแปลงการทำงานภายใน Method นั้น กล่าวคือ Class ลูก "เขียนทับ" (Override) พฤติกรรมของ Method เดิมจาก Class แม่นั่นเอง
การ Override ช่วยให้เราสามารถปรับแต่งพฤติกรรมของ Class ที่สืบทอดมาให้เหมาะสมกับความต้องการของ Class ลูกแต่ละตัว โดยไม่ต้องแก้ไข Class แม่โดยตรง ซึ่งเป็นหลักการสำคัญของการเขียนโปรแกรมเชิงวัตถุที่ยืดหยุ่นและง่ายต่อการบำรุงรักษา
การสร้าง Override Methods
การสร้าง Override Method ทำได้ง่ายมาก เพียงแค่สร้าง Method ใน Class ลูก โดยให้มีชื่อและ Parameter เหมือนกับ Method ที่ต้องการ Override ใน Class แม่
ตัวอย่าง:
<?php
class Animal {
public function makeSound() {
echo "Generic animal sound";
}
}
class Dog extends Animal {
public function makeSound() {
echo "Woof!";
}
}
$animal = new Animal();
$dog = new Dog();
$animal->makeSound(); // Output: Generic animal sound
$dog->makeSound(); // Output: Woof!
?>
คำอธิบาย:
- เราสร้าง Class ชื่อ
Animalซึ่งมี Method ชื่อmakeSound()ที่แสดงข้อความ "Generic animal sound" - เราสร้าง Class ชื่อ
Dogซึ่งสืบทอดมาจากAnimal - ใน Class
Dogเราสร้าง Method ชื่อmakeSound()อีกครั้ง โดยให้แสดงข้อความ "Woof!" แทน - เมื่อเราสร้าง Object จาก Class
Animalและเรียกใช้makeSound()เราจะได้ผลลัพธ์เป็น "Generic animal sound" - แต่เมื่อเราสร้าง Object จาก Class
Dogและเรียกใช้makeSound()เราจะได้ผลลัพธ์เป็น "Woof!" แทน เนื่องจาก Method ใน ClassDogได้ทำการ Override Method ใน ClassAnimalแล้ว
การใช้งาน Override Methods
การใช้งาน Override Methods เป็นไปโดยอัตโนมัติ เมื่อเราเรียกใช้ Method ที่ถูก Override ใน Object ของ Class ลูก PHP จะทำการเรียกใช้ Method ที่อยู่ใน Class ลูกก่อนเสมอ หากไม่มี Method นั้นอยู่ใน Class ลูก PHP จึงจะไปค้นหา Method นั้นใน Class แม่ (Parent Class) หรือ Class แม่ของ Class แม่ (Grandparent Class) ไล่ขึ้นไปเรื่อยๆ
ตัวอย่างเพิ่มเติม:
<?php
class Vehicle {
protected $brand;
public function __construct($brand) {
$this->brand = $brand;
}
public function honk() {
echo "Beep beep!";
}
public function displayBrand() {
echo "Brand: " . $this->brand;
}
}
class Car extends Vehicle {
private $model;
public function __construct($brand, $model) {
parent::__construct($brand); // เรียก Constructor ของ Parent Class
$this->model = $model;
}
public function honk() {
echo "Honk honk!"; // Override Method honk()
}
public function displayModel() {
echo "Model: " . $this->model;
}
}
$myCar = new Car("Toyota", "Camry");
$myCar->honk(); // Output: Honk honk!
$myCar->displayBrand(); // Output: Brand: Toyota
$myCar->displayModel(); // Output: Model: Camry
?>
คำอธิบาย:
- เราสร้าง Class
Vehicleซึ่งมี Properties$brandและ Methodshonk()และdisplayBrand() - เราสร้าง Class
Carซึ่งสืบทอดมาจากVehicleและมี Properties$modelและ MethoddisplayModel() - ใน Class
Carเรา Override Methodhonk()เพื่อให้แสดงข้อความ "Honk honk!" แทน "Beep beep!" - เราเรียกใช้ Constructor ของ Parent Class (
Vehicle) โดยใช้parent::__construct($brand);เพื่อกำหนดค่าให้กับ Property$brand - เมื่อเราสร้าง Object
$myCarจาก ClassCarและเรียกใช้honk()เราจะได้ผลลัพธ์เป็น "Honk honk!" ซึ่งแสดงให้เห็นว่า Method ที่ถูก Override ใน Class ลูกถูกเรียกใช้งาน - Method
displayBrand()ไม่ได้ถูก Override ดังนั้นจึงยังคงทำงานเหมือนเดิมใน Class แม่
ข้อควรจำ:
- ชื่อและ Parameter ของ Method ที่ Override ต้องเหมือนกับ Method ใน Class แม่ทุกประการ
- Access Modifier ของ Method ที่ Override ใน Class ลูก ต้องมี Access Modifier ที่เหมือนกัน หรือกว้างกว่า Access Modifier ของ Method ใน Class แม่ (เช่น ถ้า Method ใน Class แม่เป็น
protectedMethod ใน Class ลูกต้องเป็นprotectedหรือpublicเท่านั้น) - การ Override Methods เป็นเครื่องมือที่ทรงพลังในการปรับแต่งพฤติกรรมของ Class ที่สืบทอดมา แต่ควรใช้อย่างระมัดระวังเพื่อให้ Code ยังคงอ่านง่ายและเข้าใจได้
ในบทถัดไป เราจะเรียนรู้เกี่ยวกับการใช้ parent Keyword เพื่อเข้าถึง Methods ของ Class แม่ภายใน Class ลูก