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

参见

add a note

User Contributed Notes

There are no user contributed notes for this page.
To Top