본문으로 건너뛰기

เนื้อหาภาษาไทย / 태국어 콘텐츠

시스템 가이드 검수 완료된 콘텐츠

Parent Keyword: การเข้าถึง Methods ของ Class แม่

บทความนี้อธิบายการใช้งาน `parent` keyword ใน PHP ซึ่งช่วยให้ Class ลูกสามารถเข้าถึง Methods และ Properties ของ Class แม่ได้ แม้จะมีการ Override Method นั้นใน Cl...

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 แม่ด้วย parent keyword
  • Clarity: ทำให้ Code อ่านง่ายและเข้าใจได้ง่ายขึ้น เพราะบ่งบอกอย่างชัดเจนว่าเรากำลังเรียกใช้ Method ของ Class แม่

ข้อควรระวัง

แม้ว่า parent keyword จะมีประโยชน์มาก แต่ก็มีข้อควรระวังบางประการ:

  • ความสัมพันธ์แบบ Inheritance: parent keyword สามารถใช้ได้เฉพาะใน Class ที่สืบทอดมาจาก Class อื่นเท่านั้น หากใช้ใน Class ที่ไม่ได้สืบทอด จะเกิด Error
  • Visibility: parent keyword สามารถเข้าถึง 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

Office.in.th Custom Service

이 구조를 바탕으로 시스템을 개발하거나 맞춤 변경하고 싶으신가요?

Office.in.th는 엔터프라이즈 웹 애플리케이션, API 연동, 공공/민간 업무 관리 시스템을 설계·개발하며 장기 지원을 제공합니다.

소프트웨어 개발 서비스 보기