get_class
get_class(object $object = ): string
Returns the class of an object; the counterpart of instanceof - instanceof answers yes/no, get_class gives the exact name.
C C23
#include <stdio.h>
/* C has no classes - the closest pattern is a struct whose vtable-like
header carries its own type name as a plain string */
typedef struct {
const char *kind; /* manual "class name" tag */
} Object;
int main(void) {
Object d = { "Dog" };
Object a = { "Animal" };
printf("%s\n", d.kind);
printf("%s\n", a.kind);
return 0;
}
Dog Animal
C++ C++26
#include <iostream>
#include <string>
class Animal {
public:
virtual ~Animal() = default;
virtual std::string name() const { return "Animal"; }
};
class Dog : public Animal {
public:
std::string name() const override { return "Dog"; }
};
int main() {
Dog d;
Animal a;
// typeid(d).name() is NOT portable - mangled/implementation-defined,
// so classes carry the name through a virtual method
std::cout << d.name() << "\n";
std::cout << a.name() << "\n";
std::cout << (dynamic_cast<Dog*>(&d) != nullptr ? "yes" : "no") << "\n";
return 0;
}
Dog Animal yes
C# v11.0.100
class Animal { }
class Dog : Animal { }
class MainClass {
static void Main() {
object d = new Dog();
System.Console.WriteLine(d.GetType().Name); // Dog
System.Console.WriteLine(new Animal().GetType().Name);
System.Console.WriteLine(d is Dog ? "yes" : "no"); // the yes/no counterpart
}
}
Dog Animal yes
Go v1.27.4
package main
import (
"fmt"
"reflect"
)
type Animal struct{}
type Dog struct{ Animal }
func main() {
var d any = Dog{}
fmt.Println(reflect.TypeOf(d).Name()) // Dog - bare type name
fmt.Println(fmt.Sprintf("%T", d)) // main.Dog - package-qualified
}
Dog main.Dog
Java v27.0.1
class Animal { }
class Dog extends Animal { }
class Main {
public static void main(String[] args) {
Object d = new Dog();
System.out.println(d.getClass().getSimpleName()); // Dog
System.out.println(new Animal().getClass().getSimpleName());
System.out.println(d instanceof Dog ? "yes" : "no");
}
}
Dog Animal yes
JavaScript v26.13.1
class Animal {}
class Dog extends Animal {}
const d = new Dog();
console.log(d.constructor.name); // Dog
console.log(new Animal().constructor.name); // Animal
console.log(d instanceof Dog ? "yes" : "no");
Dog Animal yes
PHP v8.6.0
class Animal {
public function speak(): string { return "..."; }
}
class Dog extends Animal {
public function speak(): string { return "Woof"; }
}
$d = new Dog();
echo get_class($d), "\n"; // Dog
echo get_class(new Animal()), "\n"; // Animal
echo $d instanceof Dog ? "yes" : "no", "\n";
// since 8.3 a NO-argument call emits E_DEPRECATED - inside a class
// prefer static::class or self::class instead
Dog Animal yes
Python v3.15.2
class Animal:
pass
class Dog(Animal):
pass
d = Dog()
print(type(d).__name__) # Dog
print(type(Animal()).__name__) # Animal
print("yes" if isinstance(d, Dog) else "no")
Dog Animal yes
Rust v1.99.1
// std::any::type_name gives the fully qualified path, e.g. "crate::Dog";
// take the last segment for the bare type name
struct Animal;
struct Dog;
fn bare_name<T>() -> &'static str {
let full = std::any::type_name::<T>();
full.rsplit("::").next().unwrap_or(full)
}
fn main() {
println!("{}", bare_name::<Dog>());
println!("{}", bare_name::<Animal>());
}
Dog Animal
TypeScript v26.13.1
class Animal {}
class Dog extends Animal {}
const d = new Dog();
console.log(d.constructor.name); // Dog - same runtime answer as JS
console.log(new Animal().constructor.name);
// compile-time companion: d's static type is Dog, so this call
// is checked against the declared class
const cls: string = d.constructor.name;
console.log(cls === "Dog" ? "yes" : "no");
Dog Animal yes
Language differences
C: no classes or runtime type info - the "object class name" is a hand-written tag field (example); a real runtime registry must be built by hand.
C++: typeid(d).name() is implementation-defined (MSVC prints class Dog, Itanium ABI prints 3Dog), so the example carries the name through a virtual method; dynamic_cast is the yes/no counterpart; std::type_index allows hashing/comparison but the name stays non-portable.
C#: GetType().Name is the closest shape to get_class (example); GetType().FullName adds the namespace (PHP has no such split - its class names are global); the is operator is the instanceof counterpart.
Go: types are not classes - reflect.TypeOf(d).Name() gives the bare name (example); %T gives main.Dog with the package, equivalent to PHP's fully qualified name; on statically typed variables reflect.TypeOf works directly.
Java: getClass().getSimpleName() matches get_class (example); getName() yields the fully qualified form (inner classes make Main$Dog shapes); most idiomatic paired with instanceof.
JavaScript: d.constructor.name is the idiomatic shape (example); note constructor can be reassigned - Object.getPrototypeOf(d).constructor.name is stricter; after minification class names are unreliable - never depend on it in production.
PHP: get_class($obj) is the canonical object class name (example); since 8.3 a no-argument call emits E_DEPRECATED (use static::class inside classes); family with class_exists (table lookup) and is_a (check).
Python: type(d).__name__ (example); d.__class__.__name__ is equivalent; paired with isinstance; classes are objects, so type(d) compares directly.
Rust: no inheritance, no vtable - std::any::type_name::<T>() is the compile-time generic shape giving the full path (the example takes the last segment for the bare name); officially debug-only, "format may change" - never branch on it.
TypeScript: runtime identical to JS (example); at compile time d's static type participates in checking, and d.constructor.name types as string, not a literal - assert if you need the literal.