檢索對像或者類的父級類名

get_parent_class

(PHP 4, PHP 5, PHP 7, PHP 8)

get_parent_class檢索對像或者類的父級類名

說明

get_parent_class(object|string $object_or_class = ?): string|false

檢索對像或者類的父級類名。

參數

object_or_class

檢查的對象或者類名。如果是從對像內的方法中呼叫此函式,則此參數可選。

返回值

返回 object_or_class 是類名或者類實例的父類名稱。

注意:

如果對像沒有父類或者指定的類名不存在,則返回 false

如果在對像外部不帶參數呼叫,則返回 false

更新日誌

版本 說明
8.0.0 object_or_class 參數現在僅接受對像或者有效的類名。

範例

示例 #1 使用 get_parent_class()

<?php

class Dad {
    function 
__construct()
    {
    
// implements some logic
    
}
}

class 
Child extends Dad {
    function 
__construct()
    {
        echo 
"I'm " get_parent_class($this) , "'s son\n";
    }
}

class 
Child2 extends Dad {
    function 
__construct()
    {
        echo 
"I'm " get_parent_class('child2') , "'s son too\n";
    }
}

$foo = new child();
$bar = new child2();

?>

以上例程會輸出:

I'm Dad's son
I'm Dad's son too

參見

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *