Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> PHP

การสืบทอด (Inheritance) ใน PHP

บทนำ

Inheritance (การสืบทอด) เป็นหลักการสำคัญของ OOP ที่กำหนดความสัมพันธ์ระหว่างสองคลาส PHP รองรับ Inheritance ใน Object Model ของมัน

PHP ใช้คีย์เวิร์ด extends เพื่อสร้างความสัมพันธ์ระหว่างคลาส:

class B extends A

โดยที่:

  • A = Base Class / Parent Class (คลาสแม่/พ่อ)
  • B = Sub Class / Child Class (คลาสลูก)

Child Class จะสืบทอด:

  • Public methods/properties
  • Protected methods/properties

แต่ ไม่สืบทอด Private members

Child Class สามารถ Override (เขียนทับ) เมธอดที่สืบทอดมาได้ หากไม่ Override จะใช้การทำงานของ Parent Class

ข้อกำหนดการประกาศ

Definition ของ Parent Class ต้องอยู่ก่อน Child Class ในสคริปต์เดียวกัน (หรือใช้ Autoloading)

ตัวอย่าง Inheritance พื้นฐาน

<?php
class parentclass {
    public function publicmethod() {
        echo "This is public method of parent class\n";
    }
    protected function protectedmethod() {
        echo "This is protected method of parent class\n";
    }
    private function privatemethod() {
        echo "This is private method of parent class\n";
    }
}

class childclass extends parentclass {
    public function childmethod() {
        $this->protectedmethod();  // เข้าถึงได้
        // $this->privatemethod(); // Error - ไม่เข้าถึง private ได้
    }
}

$obj = new childclass();
$obj->publicmethod();
$obj->childmethod();
?>

ผลลัพธ์

This is public method of parent class
This is protected method of parent class
PHP Fatal error: Uncaught Error: Call to private method parentclass::privatemethod() from context 'childclass'

Method Overriding

เมื่อ Child Class นิยามเมธอดซ้ำที่สืบทอดมา การทำงานใหม่จะแทนที่เดิม:

<?php
class parentclass {
    public function publicmethod() {
        echo "This is public method of parent class\n";
    }
}

class childclass extends parentclass {
    public function publicmethod() {
        echo "public method of parent class is overridden in child class\n";
    }
}

$obj = new childclass();
$obj->publicmethod();
?>

ผลลัพธ์

public method of parent class is overridden in child class

Hierarchical Inheritance (การสืบทอดแบบลำดับชั้น)

PHP ไม่รองรับ Multiple Inheritance (extends หลายคลาส) แต่รองรับ Hierarchical Inheritance:

<?php
class A {
    function test() {
        echo "method in A class";
    }
}
class B extends A { }
class C extends B { }

$obj = new C();
$obj->test();  // เรียกเมธอดจาก A ได้
?>

ผลลัพธ์

method in A class

การเข้าถึง Parent Constructor/Method

ใช้ parent::methodName() เพื่อเรียกเมธอดของ Parent:

<?php
class A {
    public function __construct() {
        echo "Parent constructor\n";
    }
    public function greet() {
        echo "Hello from A\n";
    }
}

class B extends A {
    public function __construct() {
        parent::__construct();  // เรียก Parent Constructor
        echo "Child constructor\n";
    }
    public function greet() {
        parent::greet();        // เรียก Parent Method
        echo "Hello from B\n";
    }
}

$obj = new B();
$obj->greet();
?>

คำสั่ง final

ใช้ final เพื่อป้องกันการ Override:

  • final public function methodName() - ไม่ให้ Override เมธอดนี้
  • final class ClassName - ไม่ให้ Extend คลาสนี้