get_parent_class

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

Returns the parent class of an object or class, false at the root; makes the "one step up" of an inheritance chain a runtime-queryable fact.

C C23

C23
#include <stdio.h>

/* No inheritance in C - simulate with an embedded "parent" struct plus
   a manual tag naming it */
typedef struct {
    const char *kind;
} Animal;

typedef struct {
    Animal base; /* embedding plays the parent role */
    const char *kind;
} Dog;

int main(void) {
    Dog d = { { "Animal" }, "Dog" };
    printf("%s\n", d.base.kind); /* the "parent" tag */
    return 0;
}
Output
Animal

C++ C++26

C++26
#include <iostream>
#include <type_traits>

class Animal {};
class Dog : public Animal {};

int main() {
    // base-class NAMES are not queryable portably, but the RELATIONSHIP
    // is checked at compile time - 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;
}
Output
true
false

C# v11.0.100

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
        // the root is not "false" but System.Object - every chain ends there
        System.Console.WriteLine(typeof(Animal).BaseType.Name); // Object
    }
}
Output
Animal
Dog
Object

Go v1.27.4

v1.27.4
package main

import (
	"fmt"
	"reflect"
)

type Animal struct{}
type Dog struct{ Animal } // embedding: composition, NOT inheritance

func main() {
	// Go has no parent class - there is no inheritance. Embedding is
	// composition; the embedded type is inspectable as an ordinary field:
	fmt.Println(reflect.TypeOf(Dog{}.Animal).Name()) // Animal
	fmt.Println(reflect.TypeOf(Dog{}).NumField())    // 1 - a real field
}
Output
Animal
1

Java v27.0.1

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
        // the root is Object, never null for a class type
        System.out.println(Animal.class.getSuperclass().getSimpleName()); // Object
    }
}
Output
Animal
Dog
Object

JavaScript v26.13.1

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
// the root chain lands on Object.prototype
console.log(Object.getPrototypeOf(Animal.prototype).constructor.name); // Object
Output
Animal
Dog
Object

PHP v8.6.0

v8.6.0
class Animal {}
class Dog extends Animal {}
class Puppy extends Dog {}

echo get_parent_class(new Dog()), "\n";     // Animal - from an object
echo get_parent_class(Puppy::class), "\n";  // Dog - from a class name
// root class: false, not null - the one FALSE return in the family
echo get_parent_class(Animal::class) ? "has parent" : "(root)", "\n";
Output
Animal
Dog
(root)

Python v3.15.2

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 - the root
Output
Animal
Dog
object

Rust v1.99.1

v1.99.1
// Rust has no inheritance: there is no parent type to query, and
// embedding a struct is plain composition with no "super" relation.
struct Animal;
struct Dog {
    animal: Animal, // embedding: reach it explicitly, like any field
}

fn main() {
    // both are zero-sized, and no implicit relationship exists
    let d = Dog { animal: Animal };
    println!("{}", std::mem::size_of_val(&d.animal) == 0); // true
    println!("{}", std::mem::size_of::<Dog>() == 0);       // true
}
Output
true
true

TypeScript v26.13.1

v26.13.1
class Animal {}
class Dog extends Animal {}
class Puppy extends Dog {}

// runtime: the same prototype walk as plain 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
// compile time: `extends` checks the same relation statically
type IsChild = Puppy extends Dog ? "yes" : "no"; // "yes"
Output
Animal
Dog
Object

Language differences

C: no inheritance - "parent" is an embedded struct plus a hand-written tag (example); parent members need explicit d.base.x access.

C++: base-class NAMES are not portably queryable (typeid is implementation-defined), so the example checks the RELATIONSHIP instead - std::is_base_of_v<Base, Derived> as a compile-time boolean; C++ philosophy: relations matter more than names for generic constraints.

C#: typeof(X).BaseType.Name (example); the root returns System.Object, not false - unlike PHP's false, every chain ends at Object; an interface's BaseType is null (interfaces have no single-inheritance chain).

Go: no inheritance - embedding is composition (example); the embedded type is just an ordinary field; Dog{}.Animal is literally field access in Go, no super semantics.

Java: getSuperclass().getSimpleName() (example); the root is Object; for the full chain loop over getSuperclass() - Java has no built-in mro.

JavaScript: prototype walk via Object.getPrototypeOf(X.prototype).constructor.name (example); TS/JS extends is a single chain like PHP single inheritance; mixins do not appear on the prototype chain.

PHP: accepts both an object and a class name (example); the root returns false (the only false in the family); PHP is single inheritance + multi-interface, so the parent is always exactly one; family with get_class and class_parents (full chain).

Python: __bases__ is a tuple (multi-inheritance allows several), [0] takes the first (example); the full chain is __mro__ (C3 linearization); the root is object.

Rust: no inheritance, no parent type (the example shows embedding is a plain field); shared behavior uses traits + supertraits, but those are constraints, not parent types, and are not runtime-queryable.

TypeScript: runtime same JS prototype walk (example); compile time can assert the relation statically via conditional extends types - the extra static face TS adds over JS.