跳到主要内容

เนื้อหาภาษาไทย / 泰语内容

系统指南 内容已审核

OOP: Inheritance

บทความนี้อธิบายเรื่อง Inheritance หรือการสืบทอด ซึ่งเป็นหลักการสำคัญของ OOP ที่ช่วยให้ Class ใหม่ (Subclass) สามารถสืบทอดคุณสมบัติและพฤติกรรมจาก Class เดิม (Sup...

OOP: Inheritance

Inheritance (การสืบทอด) เป็นหนึ่งในหลักการสำคัญของ Object-Oriented Programming (OOP) ที่ช่วยให้เราสามารถสร้าง Class ใหม่โดยอิงจาก Class ที่มีอยู่แล้ว โดย Class ใหม่ (Subclass หรือ Child Class) จะสืบทอดคุณสมบัติ (Properties) และพฤติกรรม (Methods) จาก Class เดิม (Superclass หรือ Parent Class) ทำให้ลดความซ้ำซ้อนของโค้ด และเพิ่มความสามารถในการนำโค้ดกลับมาใช้ใหม่ (Code Reusability) ได้อย่างมีประสิทธิภาพ

การสร้าง Subclass ด้วย Keyword 'extends'

ใน PHP เราใช้ keyword extends เพื่อระบุว่า Class หนึ่งสืบทอดมาจาก Class อื่น ตัวอย่าง:

<?php

class Animal {
  public $name;
  public $color;

  public function __construct($name, $color) {
    $this->name = $name;
    $this->color = $color;
  }

  public function makeSound() {
    echo "Generic animal sound\n";
  }
}

class Dog extends Animal {
  public function makeSound() {
    echo "Woof!\n";
  }
}

$animal = new Animal("Generic Animal", "Brown");
$animal->makeSound(); // Output: Generic animal sound

$dog = new Dog("Buddy", "Golden");
$dog->makeSound(); // Output: Woof!

echo $dog->name . "\n"; // Output: Buddy
echo $dog->color . "\n"; // Output: Golden

?>

ในตัวอย่างนี้ Dog เป็น Subclass ของ Animal ดังนั้น Dog จึงสืบทอด Property $name และ $color รวมถึง Method makeSound() จาก Animal

เหตุผลเชิงออกแบบและการใช้งาน Inheritance

การใช้ Inheritance มีข้อดีหลายประการ:

  • Code Reusability: ลดความซ้ำซ้อนของโค้ด โดยการนำ Properties และ Methods ที่มีอยู่แล้วใน Superclass มาใช้ใน Subclass
  • Code Organization: จัดระเบียบโค้ดให้เป็นลำดับชั้น (Hierarchy) ทำให้ง่ายต่อการทำความเข้าใจและบำรุงรักษา
  • Polymorphism (หลายรูปแบบ): รองรับ Polymorphism ซึ่งเป็นหลักการที่ Object สามารถถูกมองว่าเป็น Object ของ Class อื่นได้ (จะกล่าวถึงในบทถัดไป)

อย่างไรก็ตาม การใช้ Inheritance มากเกินไปก็อาจทำให้เกิดปัญหาได้เช่นกัน:

  • Tight Coupling: Subclass ผูกติดกับ Superclass อย่างแน่นหนา หากมีการเปลี่ยนแปลงใน Superclass อาจส่งผลกระทบต่อ Subclass
  • Fragile Base Class Problem: การเปลี่ยนแปลงใน Superclass ที่ไม่คาดคิด อาจทำให้ Subclass ทำงานผิดพลาด
  • Increased Complexity: โครงสร้าง Inheritance ที่ซับซ้อน อาจทำให้ยากต่อการทำความเข้าใจและบำรุงรักษา

แนวปฏิบัติที่ดี:

  • Favor Composition over Inheritance: ในหลายกรณี การใช้ Composition (การรวม Object ของ Class อื่นๆ เข้ามาเป็นส่วนประกอบของ Class หนึ่ง) อาจเป็นทางเลือกที่ดีกว่า Inheritance เพราะช่วยลด Coupling และเพิ่มความยืดหยุ่น
  • Liskov Substitution Principle (LSP): Subclass ควรสามารถใช้แทน Superclass ได้โดยไม่ทำให้โปรแกรมทำงานผิดพลาด
  • Keep Inheritance Hierarchies Shallow: หลีกเลี่ยงการสร้าง Inheritance Hierarchy ที่ลึกเกินไป เพราะจะทำให้โค้ดซับซ้อนและยากต่อการบำรุงรักษา

การ Override Methods

Subclass สามารถ override (เขียนทับ) Methods ที่สืบทอดมาจาก Superclass ได้ เพื่อปรับเปลี่ยนพฤติกรรมให้เหมาะสมกับ Subclass ตัวอย่าง:

<?php

class Animal {
  public function makeSound() {
    echo "Generic animal sound\n";
  }
}

class Cat extends Animal {
  public function makeSound() {
    echo "Meow!\n";
  }
}

$animal = new Animal();
$animal->makeSound(); // Output: Generic animal sound

$cat = new Cat();
$cat->makeSound(); // Output: Meow!

?>

ในตัวอย่างนี้ Cat override Method makeSound() จาก Animal ทำให้เมื่อเรียก $cat->makeSound() จะได้ผลลัพธ์เป็น "Meow!" แทน "Generic animal sound"

การเข้าถึง Properties และ Methods ของ Superclass ด้วย parent::

ใน Subclass เราสามารถเข้าถึง Properties และ Methods ของ Superclass ได้โดยใช้ keyword parent:: ตัวอย่าง:

<?php

class Animal {
  public $name;

  public function __construct($name) {
    $this->name = $name;
  }

  public function makeSound() {
    echo "Generic animal sound\n";
  }
}

class Dog extends Animal {
  public $breed;

  public function __construct($name, $breed) {
    parent::__construct($name); // เรียก Constructor ของ Superclass
    $this->breed = $breed;
  }

  public function makeSound() {
    parent::makeSound(); // เรียก makeSound() ของ Superclass
    echo "Woof!\n";
  }
}

$dog = new Dog("Buddy", "Golden Retriever");
$dog->makeSound();
// Output: Generic animal sound
// Output: Woof!

echo $dog->name . "\n"; // Output: Buddy
echo $dog->breed . "\n"; // Output: Golden Retriever

?>

ใน Constructor ของ Dog เราเรียก Constructor ของ Animal ด้วย parent::__construct($name) เพื่อกำหนดค่าให้กับ Property $name ที่สืบทอดมา นอกจากนี้ ใน Method makeSound() ของ Dog เรายังเรียก makeSound() ของ Animal ด้วย parent::makeSound() ก่อนที่จะแสดง "Woof!"

Visibility และ Inheritance

Visibility (Public, Protected, Private) มีผลต่อการสืบทอด ดังนี้:

  • Public: Properties และ Methods ที่เป็น Public สามารถเข้าถึงได้จากทุกที่ (ภายใน Class, Subclass, และภายนอก Class)
  • Protected: Properties และ Methods ที่เป็น Protected สามารถเข้าถึงได้จากภายใน Class และ Subclass เท่านั้น
  • Private: Properties และ Methods ที่เป็น Private สามารถเข้าถึงได้จากภายใน Class เท่านั้น ไม่สามารถเข้าถึงได้จาก Subclass หรือภายนอก Class

ตัวอย่าง:

<?php

class Animal {
  public $publicProperty = "Public";
  protected $protectedProperty = "Protected";
  private $privateProperty = "Private";

  public function getPrivateProperty() {
    return $this->privateProperty;
  }
}

class Dog extends Animal {
  public function accessProperties() {
    echo $this->publicProperty . "\n"; // สามารถเข้าถึงได้
    echo $this->protectedProperty . "\n"; // สามารถเข้าถึงได้
    //echo $this->privateProperty . "\n"; // Error: Cannot access private property
    echo parent::getPrivateProperty() . "\n"; // สามารถเข้าถึงได้ผ่าน Public method ใน Parent Class
  }
}

$dog = new Dog();
$dog->accessProperties();
// Output: Public
// Output: Protected
// Output: Private

//echo $dog->privateProperty; // Error: Cannot access private property from outside the class

?>

สรุป: Inheritance เป็นเครื่องมือที่มีประสิทธิภาพในการสร้างโค้ดที่สามารถนำกลับมาใช้ใหม่ได้ และจัดระเบียบโค้ดให้เป็นลำดับชั้น อย่างไรก็ตาม ควรใช้ Inheritance อย่างระมัดระวัง และพิจารณาทางเลือกอื่น เช่น Composition หากเหมาะสมกว่า

Office.in.th Custom Service

想基于此结构开发或定制系统?

Office.in.th 承接企业级 Web Application、API Integration 以及政府/企业管理系统的设计与开发,并提供长期支持。

查看软件开发服务