get_parent_class
get_parent_class(object|string $object_or_class = ): string|false
返回对象或类的父类名,根类返回 false;把继承链的「上一步」变成可查询的运行期事实。
C C23
#include <stdio.h>
/* C 无继承——用「内嵌父结构体 + 手写标签」模拟 */
typedef struct {
const char *kind;
} Animal;
typedef struct {
Animal base; /* 内嵌扮演父类角色 */
const char *kind;
} Dog;
int main(void) {
Dog d = { { "Animal" }, "Dog" };
printf("%s\n", d.base.kind); /* 「父类」标签 */
return 0;
}
Animal
C++ C++26
#include <iostream>
#include <type_traits>
class Animal {};
class Dog : public Animal {};
int main() {
// 基类「名字」不可移植查询,但「关系」可在编译期检查——
// is_base_of<Base, Derived>
std::cout << std::boolalpha
<< std::is_base_of_v<Animal, Dog> << "\n"; // true
std::cout << std::is_base_of_v<Dog, Animal> << "\n"; // false
return 0;
}
true false
C# v11.0.100
class Animal { }
class Dog : Animal { }
class Puppy : Dog { }
class MainClass {
static void Main() {
System.Console.WriteLine(typeof(Dog).BaseType.Name); // Animal
System.Console.WriteLine(typeof(Puppy).BaseType.Name); // Dog
// 根不是「false」而是 System.Object——所有链终于此
System.Console.WriteLine(typeof(Animal).BaseType.Name); // Object
}
}
Animal Dog Object
Go v1.27.4
package main
import (
"fmt"
"reflect"
)
type Animal struct{}
type Dog struct{ Animal } // 内嵌:是组合,不是继承
func main() {
// Go 没有父类——没有继承。内嵌是组合;被内嵌类型就是
// 一个普通字段,可正常反射检查:
fmt.Println(reflect.TypeOf(Dog{}.Animal).Name()) // Animal
fmt.Println(reflect.TypeOf(Dog{}).NumField()) // 1——真实字段
}
Animal 1
Java v27.0.1
class Animal { }
class Dog extends Animal { }
class Puppy extends Dog { }
class Main {
public static void main(String[] args) {
System.out.println(Dog.class.getSuperclass().getSimpleName()); // Animal
System.out.println(Puppy.class.getSuperclass().getSimpleName()); // Dog
// 根是 Object,类类型永远不会是 null
System.out.println(Animal.class.getSuperclass().getSimpleName()); // Object
}
}
Animal Dog Object
JavaScript v26.13.1
class Animal {}
class Dog extends Animal {}
class Puppy extends Dog {}
console.log(Object.getPrototypeOf(Dog.prototype).constructor.name); // Animal
console.log(Object.getPrototypeOf(Puppy.prototype).constructor.name); // Dog
// 根链落在 Object.prototype
console.log(Object.getPrototypeOf(Animal.prototype).constructor.name); // Object
Animal Dog Object
PHP v8.6.0
class Animal {}
class Dog extends Animal {}
class Puppy extends Dog {}
echo get_parent_class(new Dog()), "\n"; // Animal——从对象取
echo get_parent_class(Puppy::class), "\n"; // Dog——从类名取
// 根类:返回 false 而非 null——族中唯一的 FALSE 返回
echo get_parent_class(Animal::class) ? "has parent" : "(root)", "\n";
Animal Dog (root)
Python v3.15.2
class Animal:
pass
class Dog(Animal):
pass
class Puppy(Dog):
pass
print(Dog.__bases__[0].__name__) # Animal
print(Puppy.__bases__[0].__name__) # Dog
print(Animal.__bases__[0].__name__) # object——根
Animal Dog object
Rust v1.99.1
// Rust 无继承:没有可查询的父类型;内嵌结构体就是普通组合,
// 没有任何「super」关系。
struct Animal;
struct Dog {
animal: Animal, // 内嵌:像普通字段一样显式访问
}
fn main() {
// 两者都是零大小,且不存在隐式关系
let d = Dog { animal: Animal };
println!("{}", std::mem::size_of_val(&d.animal) == 0); // true
println!("{}", std::mem::size_of::<Dog>() == 0); // true
}
true true
TypeScript v26.13.1
class Animal {}
class Dog extends Animal {}
class Puppy extends Dog {}
// 运行期:与纯 JS 相同的原型链上溯
console.log(Object.getPrototypeOf(Dog.prototype).constructor.name); // Animal
console.log(Object.getPrototypeOf(Puppy.prototype).constructor.name); // Dog
console.log(Object.getPrototypeOf(Animal.prototype).constructor.name); // Object
// 编译期:`extends` 约束在静态侧检查同一关系
type IsChild = Puppy extends Dog ? "yes" : "no"; // "yes"
Animal Dog Object
语言差异说明
C:无继承——「父」只能用内嵌结构体 + 手写标签模拟(示例);访问父成员要显式写 d.base.x。
C++:基类名不可移植查询(typeid 实现定义),示例改查「关系」——std::is_base_of_v<Base, Derived> 编译期布尔;这是 C++ 的哲学:关系比名字更常用于泛型约束。
C#:typeof(X).BaseType.Name(示例);根类返回 System.Object 而非 false——与 PHP 的 false 收尾不同,所有链终于 Object;接口的 BaseType 为 null(接口没有单继承链)。
Go:无继承——内嵌是组合(示例),被内嵌类型只是普通字段;Dog{}.Animal 这种「路径」在 Go 里就是字段访问,没有 super 语义。
Java:getSuperclass().getSimpleName()(示例);根是 Object;要「全链」用循环上溯或 getSuperclass() 迭代,Java 无内置 mro。
JavaScript:原型链上溯 Object.getPrototypeOf(X.prototype).constructor.name(示例);TS/JS 的 extends 单链与 PHP 单继承一致;注意接口(mixin)不在原型链上。
PHP:对象与类名两种入参(示例);根类返回 false(族中唯一 false);PHP 单继承 + 接口多实现,父类只有「一个」;与 get_class/class_parents(全链)成族。
Python:__bases__ 是元组(多继承可多元素),[0] 取首个(示例);全链看 __mro__(C3 线性化);根是 object。
Rust:无继承无父类型(示例演示内嵌只是普通字段);要共享行为用 trait + supertrait,但那是「约束」不是「父类型」,运行期无法查询。
TypeScript:运行期同 JS 原型链(示例);编译期 extends 条件类型可静态断言关系——这是 TS 比 JS 多出的静态面。