Skip to main content

เนื้อหาภาษาไทย / Thai content

System guide Content reviewed

Abstract Methods: Methods ที่ต้อง Implement

บทความนี้อธิบายถึง Abstract Methods ใน PHP ซึ่งเป็น methods ที่ไม่มี implementation ใน Abstract Class และบังคับให้ Class ลูกต้อง implement methods เหล่านั้น Abs...

Abstract Methods: Methods ที่ต้อง Implement

จากบทที่แล้วที่เราได้เรียนรู้เกี่ยวกับ Abstract Classes กันไปแล้ว ในบทนี้เราจะมาเจาะลึกถึง Abstract Methods ซึ่งเป็นองค์ประกอบสำคัญที่ทำให้ Abstract Classes มีประโยชน์และทรงพลังในการออกแบบโปรแกรมเชิงวัตถุ (OOP) ใน PHP

ความหมายของ Abstract Methods

Abstract Methods คือ Methods ที่ถูกประกาศไว้ใน Abstract Class แต่ไม่มีการกำหนด Implementation หรือ Code ที่จะทำงานจริง สิ่งที่ Abstract Method ทำคือการ "บังคับ" ให้ Class ลูก (Child Class) ที่สืบทอดมาจาก Abstract Class นั้นๆ จะต้องทำการ Implement หรือเขียน Code สำหรับ Method นั้นๆ เอง

ลองนึกภาพว่า Abstract Class คือ "พิมพ์เขียว" (Blueprint) ของบ้าน และ Abstract Methods คือส่วนประกอบบางอย่างของบ้านที่ "ต้องมี" แต่พิมพ์เขียวไม่ได้บอกว่าส่วนประกอบนั้นจะต้องเป็นแบบไหน เช่น "ห้องครัว" พิมพ์เขียวบอกว่าบ้านต้องมีห้องครัว แต่ไม่ได้บอกว่าห้องครัวจะต้องมีขนาดเท่าไหร่ มีเคาน์เตอร์แบบไหน หรือมีอุปกรณ์อะไรบ้าง หน้าที่ในการออกแบบรายละเอียดของห้องครัวจึงเป็นของคนที่สร้างบ้านตามพิมพ์เขียว (Class ลูก)

การประกาศ Abstract Methods

ในการประกาศ Abstract Method เราจะใช้ Keyword abstract นำหน้า Method และที่สำคัญคือ Abstract Method จะต้องไม่มี Body (Body คือส่วนของ Code ที่อยู่ในเครื่องหมาย {}) แต่จะจบด้วยเครื่องหมาย ; แทน

รูปแบบการประกาศ:

abstract public function methodName($argument1, $argument2);

ข้อควรจำ:

  • Abstract Method จะต้องถูกประกาศภายใน Abstract Class เท่านั้น
  • Abstract Method จะต้องมี Access Modifier (public, protected) กำหนดไว้
  • Abstract Method จะไม่มี Body ({})

ตัวอย่าง:

abstract class Animal {
  // Abstract Method ที่บังคับให้ Class ลูกต้อง Implement method ชื่อ makeSound
  abstract public function makeSound();

  public function eat() {
    echo "สัตว์กำลังกินอาหาร";
  }
}

จากตัวอย่างข้างต้น Animal เป็น Abstract Class และ makeSound() เป็น Abstract Method ซึ่งหมายความว่า Class ใดๆ ที่สืบทอดมาจาก Animal จะต้องมี Method ชื่อ makeSound() และต้องกำหนด Code ที่จะทำงานเมื่อ Method นี้ถูกเรียกใช้

การ Implement Abstract Methods

เมื่อ Class ลูกสืบทอดมาจาก Abstract Class ที่มี Abstract Methods Class ลูกจะต้องทำการ Implement Abstract Methods เหล่านั้น มิฉะนั้น Class ลูกก็จะกลายเป็น Abstract Class ไปด้วย (และจะต้องถูกประกาศเป็น abstract class)

การ Implement หมายถึงการเขียน Code ภายใน Method ที่มีชื่อและ Argument เหมือนกับ Abstract Method ใน Class แม่ (Abstract Class)

ตัวอย่าง:

abstract class Animal {
  abstract public function makeSound();

  public function eat() {
    echo "สัตว์กำลังกินอาหาร";
  }
}

class Dog extends Animal {
  // Implement Abstract Method makeSound()
  public function makeSound() {
    echo "Woof!";
  }
}

class Cat extends Animal {
  // Implement Abstract Method makeSound()
  public function makeSound() {
    echo "Meow!";
  }
}

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

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

ในตัวอย่างนี้:

  • Dog และ Cat เป็น Class ลูกที่สืบทอดมาจาก Animal
  • ทั้ง Dog และ Cat ทำการ Implement Abstract Method makeSound() โดยกำหนด Code ที่แตกต่างกัน
  • เมื่อเราสร้าง Object จาก Dog และ Cat และเรียก Method makeSound() เราจะได้ผลลัพธ์ที่แตกต่างกันตาม Implementation ของแต่ละ Class

กรณีใช้งานจริงและข้อผิดพลาดที่พบบ่อย

กรณีใช้งานจริง:

ลองพิจารณาระบบการคำนวณพื้นที่ของรูปทรงต่างๆ เราสามารถสร้าง Abstract Class ชื่อ Shape และกำหนด Abstract Method ชื่อ calculateArea() เพื่อบังคับให้ Class ลูกที่ represents รูปทรงต่างๆ (เช่น Circle, Rectangle, Triangle) ต้อง Implement Method calculateArea() และกำหนดสูตรการคำนวณพื้นที่ของตัวเอง

abstract class Shape {
  abstract public function calculateArea();
}

class Circle extends Shape {
  private $radius;

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

  public function calculateArea() {
    return pi() * $this->radius * $this->radius;
  }
}

class Rectangle extends Shape {
  private $width;
  private $height;

  public function __construct($width, $height) {
    $this->width = $width;
    $this->height = $height;
  }

  public function calculateArea() {
    return $this->width * $this->height;
  }
}

$circle = new Circle(5);
echo "Area of Circle: " . $circle->calculateArea() . "\n"; // Output: Area of Circle: 78.539816339745

$rectangle = new Rectangle(4, 6);
echo "Area of Rectangle: " . $rectangle->calculateArea() . "\n"; // Output: Area of Rectangle: 24

ข้อผิดพลาดที่พบบ่อย:

  1. ลืม Implement Abstract Method: หาก Class ลูกไม่ได้ Implement Abstract Method ทั้งหมดที่ Class แม่กำหนดไว้ PHP จะแสดง Error
  2. // Error: Class Incomplete contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (Shape::calculateArea)
    class Incomplete extends Shape {
      // ...
    }
    
  3. Implementation ไม่ถูกต้อง: ชื่อ Method หรือ Argument ไม่ตรงกับ Abstract Method ใน Class แม่
  4. abstract class Shape {
      abstract public function calculateArea($unit);
    }
    
    class Square extends Shape {
      // Error: Method Square::calculateArea() does not match signature of Shape::calculateArea($unit)
      public function calculateArea() {
        return $this->side * $this->side;
      }
    }
    
  5. พยายามสร้าง Object จาก Abstract Class: ไม่สามารถสร้าง Object จาก Abstract Class ได้โดยตรง
  6. // Error: Cannot instantiate abstract class Shape
    $shape = new Shape();
    

สรุป

Abstract Methods เป็นเครื่องมือที่มีประโยชน์ในการบังคับให้ Class ลูกต้อง Implement Methods ที่จำเป็น ทำให้ Code มีโครงสร้างที่ชัดเจนและง่ายต่อการบำรุงรักษา การใช้ Abstract Methods ร่วมกับ Abstract Classes ช่วยให้เราสามารถสร้าง Framework หรือ Library ที่มีความยืดหยุ่นและสามารถปรับเปลี่ยนให้เข้ากับความต้องการที่แตกต่างกันได้

ในบทถัดไป เราจะมาเรียนรู้เกี่ยวกับ Static Properties และ Methods ซึ่งเป็นอีกหนึ่ง Feature ที่น่าสนใจของ OOP ใน PHP

Office.in.th Custom Service

Want to build or adapt a system from this structure?

Office.in.th designs and develops enterprise web applications, API integrations, and public- and private-sector management systems, with long-term support.

See our software development service