ReflectionClass
new ReflectionClass(object|string $objectOrClass)
Runtime introspection of a class: reads the class name, method list, and property list.
C C23
#include <stdio.h>
typedef struct {
int x;
int y;
} Point;
/* C has no reflection: "field/method listings" are a hand-maintained descriptor table */
static void point_move(Point *p, int dx, int dy) {
p->x += dx;
p->y += dy;
}
int main(void) {
(void)point_move; /* descriptor-table entry, referenced via hand table only */
printf("Point\n");
printf("methods: point_move (hand-written)\n");
printf("properties: x, y (hand-written)\n");
return 0;
}
Point methods: point_move (hand-written) properties: x, y (hand-written)
C++ C++26
#include <iostream>
struct Point {
int x = 0;
int y = 0;
void move_by(int dx, int dy) {
x += dx;
y += dy;
}
};
int main() {
// no runtime reflection: listings stay hand-written
// (C++26's P2996 static reflection is compile-time only and still landing)
std::cout << "Point\n";
std::cout << "methods: move_by (hand-written)\n";
std::cout << "properties: x, y (hand-written)\n";
return 0;
}
Point methods: move_by (hand-written) properties: x, y (hand-written)
C# v11.0.100
using System;
using System.Linq;
using System.Reflection;
Type t = typeof(Point); // DeclaredOnly keeps inherited members out
Console.WriteLine(t.Name);
Console.WriteLine("methods: " + string.Join(", ",
t.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)
.Where(m => !m.IsSpecialName) // skip compiler-generated get_X/set_X accessors
.Select(m => m.Name)));
Console.WriteLine("properties: " + string.Join(", ",
t.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)
.Select(p => p.Name)));
class Point
{
public int X { get; set; } = 0;
public int Y { get; set; } = 0;
public void Move(int dx, int dy)
{
X += dx;
Y += dy;
}
}
Point methods: Move properties: X, Y
Go v1.27.4
package main
import (
"fmt"
"reflect"
"strings"
)
type Point struct {
X int
Y int
}
func (p Point) Move(dx, dy int) {
p.X += dx
p.Y += dy
}
func main() {
// reflect works on exported names only; a pointer type's method set
// includes both value- and pointer-receiver methods
t := reflect.TypeOf(&Point{})
fmt.Println(t.Elem().Name())
var methods []string
for i := 0; i < t.NumMethod(); i++ {
methods = append(methods, t.Method(i).Name)
}
fmt.Println("methods:", strings.Join(methods, ", "))
var fields []string
pointType := t.Elem()
for i := 0; i < pointType.NumField(); i++ {
fields = append(fields, pointType.Field(i).Name)
}
fmt.Println("properties:", strings.Join(fields, ", "))
}
Point methods: Move properties: X, Y
Java v27.0.1
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.stream.Collectors;
public class Main {
static class Point {
int x = 0;
int y = 0;
void move(int dx, int dy) {
x += dx;
y += dy;
}
}
public static void main(String[] args) {
Class<Point> c = Point.class; // getDeclared*: declared only, any visibility
System.out.println(c.getSimpleName());
System.out.println("methods: " + Arrays.stream(c.getDeclaredMethods())
.map(Method::getName).collect(Collectors.joining(", ")));
System.out.println("properties: " + Arrays.stream(c.getDeclaredFields())
.map(Field::getName).collect(Collectors.joining(", ")));
}
}
Point methods: move properties: x, y
JavaScript v26.13.1
// JS has no formal reflection API: introspect objects and prototypes instead
class Point {
x = 0;
y = 0;
move(dx, dy) {
this.x += dx;
this.y += dy;
}
}
const p = new Point();
console.log(p.constructor.name);
const methods = Object.getOwnPropertyNames(Point.prototype)
.filter((n) => n !== "constructor");
console.log(`methods: ${methods.join(", ")}`);
console.log(`properties: ${Object.keys(p).join(", ")}`);
Point methods: move properties: x, y
PHP v8.6.0
class Point {
public int $x = 0;
public int $y = 0;
public function move(int $dx, int $dy): void {
$this->x += $dx;
$this->y += $dy;
}
}
// ReflectionClass reads the class as data: name, method list, property list
$rc = new ReflectionClass(Point::class);
echo $rc->getShortName(), "\n";
echo 'methods: ', implode(', ', array_map(
fn ($m) => $m->getName(), $rc->getMethods()
)), "\n";
echo 'properties: ', implode(', ', array_map(
fn ($p) => $p->getName(), $rc->getProperties()
)), "\n";
Point methods: move properties: x, y
Python v3.15.2
import inspect
class Point:
x: int = 0
y: int = 0
def move(self, dx: int, dy: int) -> None:
self.x += dx
self.y += dy
# inspect lists declared functions; class annotations list declared fields
print(Point.__name__)
print("methods:", ", ".join(
n for n, _ in inspect.getmembers(Point, inspect.isfunction)
if not n.startswith("__")
))
print("properties:", ", ".join(Point.__annotations__))
Point methods: move properties: x, y
Rust v1.99.1
use std::any::type_name;
struct Point {
x: i32,
y: i32,
}
impl Point {
fn move_by(&mut self, dx: i32, dy: i32) {
self.x += dx;
self.y += dy;
}
}
fn main() {
// Rust has NO runtime reflection: type_name is a compile-time string,
// and method/field lists below are hand-written, not introspected
println!("{}", type_name::<Point>());
println!("methods: move_by (hand-written)");
println!("properties: x, y (hand-written)");
}
main::Point methods: move_by (hand-written) properties: x, y (hand-written)
TypeScript v26.13.1
// TS adds compile-time type metadata only: no runtime reflection either,
// so introspect the JavaScript object shape instead
class Point {
x = 0;
y = 0;
move(dx: number, dy: number): void {
this.x += dx;
this.y += dy;
}
}
const p = new Point();
console.log(p.constructor.name);
const methods = Object.getOwnPropertyNames(Point.prototype)
.filter((n) => n !== "constructor");
console.log(`methods: ${methods.join(", ")}`);
console.log(`properties: ${Object.keys(p).join(", ")}`);
Point methods: move properties: x, y
Language differences
The reflection-capability spectrum (the core teaching point of this entry): languages that keep full type metadata at runtime introspect the most; compiled languages without runtime metadata must approximate at compile time or maintain listings by hand —
PHP: ReflectionClass is a first-class API (example): class name/methods/properties/modifiers/parameters/attributes are all readable, plus dynamic instantiation, invocation, and visibility tweaking — the backbone of frameworks and IDE navigation.
C: no reflection at all (the example shows the hand-maintained descriptor idiom); type information is gone after compilation — at runtime you cannot even ask what type a pointer points to.
C++: no runtime reflection either (example); typeid().name() is implementation-defined (gcc/clang emit mangled names) and lists no members; C++26 has folded P2996 compile-time static reflection into the draft while mainstream toolchains are still landing it — the direction is codegen at compile time, not runtime queries.
C#: typeof/GetType + System.Reflection (example) is on par with Java: methods/properties/constructors/Attributes all readable and invocable, with BindingFlags filtering visibility (the example keeps DeclaredOnly public instance members to skip compiler-generated get_Xxx accessors).
Go: the reflect package (example) reads struct fields and methods but with edges — unexported fields are invisible, method sets distinguish value vs pointer receivers (the example uses reflect.TypeOf(&Point{}) so Move shows up), and the performance cost is real; the Go idiom is less reflection, more interface assertions.
Java: the Class object (example) with getDeclaredMethods/getDeclaredFields, and setAccessible can even cross private — dynamic proxies, DI containers, and JUnit all rest on it.
JavaScript: there is no formal "reflection API" as such (the example approximates with object/prototype introspection); Object.keys lists only own enumerable properties — methods live on the prototype and need getOwnPropertyNames(Point.prototype).
Python: the inspect module (example) plus __dict__/__annotations__; dynamic attributes mean introspection is forever an approximation — instance attributes added at runtime never show up in the class annotations, which is exactly the dynamic-vs-static metadata divide.
Rust: firmly no runtime reflection (the example is honest about it): types keep no name/member listing after compilation, std::any::type_name is a compile-time string and std::any::Any only downcasts; the "reflection" of serde/derive macros is entirely compile-time codegen — the compiled-vs-dynamic capability divide in its purest form.
TypeScript: type information exists only at compile time (the example matches JS); interface/type vanish after compilation — runtime metadata needs decorators (experimental) or a hand-written mapping table.