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

ฟังก์ชัน get_parent_class() ใน PHP

คำอธิบาย

ฟังก์ชัน get_parent_class() ดึงชื่อคลาสแม่ (parent class) ของออบเจ็กต์หรือคลาสที่ระบุ

รูปแบบการใช้งาน (Syntax)

get_parent_class(object|class)

พารามิเตอร์

  • object|class — ออบเจ็กต์หรือชื่อคลาส

ค่าที่คืนกลับ

คืนค่าชื่อคลาสแม่ (string) หรือ FALSE หากไม่มีคลาสแม่

ตัวอย่างการใช้งาน

<?php
class Animal {
    function __construct() {}
}

class Dog extends Animal {
    function __construct() {
        echo "I'm " . get_parent_class($this) . "'s child\n";
    }
}

class Cat extends Animal {
    function __construct() {
        echo "I'm " . get_parent_class('Dog') . "'s sibling\n";
    }
}

$dog = new Dog();
$cat = new Cat();
?>

ผลลัพธ์

I'm Animal's child
I'm Animal's sibling