class_implements / class_parents / class_uses
class_implements(object|string $object_or_class, bool $autoload = true): array|false / class_parents(object|string $object_or_class, bool $autoload = true): array|false / class_uses(object|string $object_or_class, bool $autoload = true): array|false
The SPL trio: the interfaces a class implements, its parent chain, and its traits, three lists in one family - the full-family portrait of inheritance relations.
C C23
#include <stdio.h>
/* No interfaces/inheritance: C code emulates them with embedded
structs and hand-maintained tags */
typedef struct { const char *name; } Jsonable;
typedef struct Base {
const char *kind;
const Jsonable *json; /* "implements Jsonable" */
} Base;
typedef struct {
Base base; /* "extends Base" */
} Admin;
int main(void) {
static const Jsonable j = { "Jsonable" };
Admin a = { { "Base", &j } };
printf("%s %s\n", a.base.kind, a.base.json->name);
return 0;
}
Base Jsonable
C++ C++26
#include <iostream>
#include <type_traits>
class Jsonable { public: virtual ~Jsonable() = default; };
class Loggable {};
class Base : public Jsonable, public Loggable {};
class Admin : public Base {};
int main() {
// relationship probes are compile-time, names are not portable:
std::cout << std::boolalpha
<< std::is_base_of_v<Jsonable, Admin> << "\n"; // true
std::cout << std::is_base_of_v<Base, Admin> << "\n"; // true
std::cout << std::is_base_of_v<Loggable, Jsonable> << "\n"; // false
return 0;
}
true true false
C# v11.0.100
using System.Linq;
interface IJsonable { }
interface ILoggable { }
class Base : IJsonable, ILoggable { }
class Admin : Base { }
class MainClass {
static void Main() {
// interfaces: ask the declaring class (Admin inherits them via Base)
var ifaces = typeof(Base).GetInterfaces().Select(i => i.Name).OrderBy(n => n);
System.Console.WriteLine(string.Join(",", ifaces));
// parents: the single base type
System.Console.WriteLine(typeof(Admin).BaseType.Name); // Base
}
}
IJsonable,ILoggable Base
Go v1.27.4
package main
import "fmt"
type Jsonable interface{ ToJSON() string }
type Loggable interface{ Log() }
type Base struct{}
func (Base) ToJSON() string { return "{}" }
func main() {
// interfaces are satisfied IMPLICITLY: nothing declares it.
// You can only ask "does X satisfy I?", never list all of them:
var b any = Base{}
_, isJSON := b.(Jsonable)
_, isLog := b.(Loggable)
fmt.Println(isJSON, isLog) // true false
// the compile-time assertion idiom:
var _ Jsonable = Base{}
fmt.Println("Base satisfies Jsonable")
}
true false Base satisfies Jsonable
Java v27.0.1
import java.util.Arrays;
interface Jsonable { }
interface Loggable { }
class Base implements Jsonable, Loggable { }
class Admin extends Base { }
class Main {
public static void main(String[] args) {
// getInterfaces() lists DIRECTLY declared interfaces only
var ifaces = Arrays.stream(Base.class.getInterfaces())
.map(Class::getSimpleName).sorted().toArray(String[]::new);
System.out.println(String.join(",", ifaces));
System.out.println(Admin.class.getSuperclass().getSimpleName()); // Base
}
}
Jsonable,Loggable Base
JavaScript v26.13.1
class Base {}
class Admin extends Base {}
// no interface metadata: walk the prototype chain for "parents"
const chain = [];
let proto = Object.getPrototypeOf(Admin.prototype);
while (proto && proto.constructor.name !== "Object") {
chain.push(proto.constructor.name);
proto = Object.getPrototypeOf(proto);
}
console.log(chain.join(",")); // Base
Base
PHP v8.6.0
interface Jsonable { public function toJson(): string; }
interface Loggable {}
trait HasId { public int $id = 7; }
class ApiResource implements Jsonable, Loggable {
use HasId;
public function toJson(): string { return "{}"; }
}
class Admin extends ApiResource {}
echo json_encode(array_keys(class_implements(ApiResource::class))), "\n";
echo json_encode(array_keys(class_parents(Admin::class))), "\n";
echo json_encode(array_keys(class_uses(ApiResource::class))), "\n";
// class_uses on a trait-less class: empty array, not false
echo json_encode(class_uses(DateTime::class)), "\n";
["Jsonable","Loggable"] ["ApiResource"] ["HasId"] []
Python v3.15.2
class Jsonable:
pass
class Loggable:
pass
class Base(Jsonable, Loggable):
pass
class Admin(Base):
pass
# Python has no explicit interface keyword - mixins play that role;
# __mro__ is the full chain (C3 linearized), __bases__ the direct parents
print([c.__name__ for c in Admin.__mro__])
print([c.__name__ for c in Admin.__bases__])
['Admin', 'Base', 'Jsonable', 'Loggable', 'object'] ['Base']
Rust v1.99.1
// traits are checked at compile time; there is no runtime list
trait Jsonable {}
trait Loggable {}
struct Base;
struct Admin;
impl Jsonable for Base {}
impl Loggable for Base {}
impl Jsonable for Admin {} // Admin "implements" Jsonable directly
fn require_json<T: Jsonable>(_x: &T) {}
fn main() {
require_json(&Admin); // compiles: Admin satisfies Jsonable
println!("Admin: Jsonable ok");
// Loggable for Admin would fail to compile - no runtime probe exists
}
Admin: Jsonable ok
TypeScript v26.13.1
interface Jsonable {}
interface Loggable {}
class Base implements Jsonable, Loggable {}
class Admin extends Base {}
// interfaces are compile-time only - no runtime trace survives;
// the parent link is the only runtime-walkable relationship:
console.log(Object.getPrototypeOf(Admin.prototype).constructor.name); // Base
Base
Language differences
C: no interface/inheritance concepts (the example emulates with embedding + tags); the "relation list" is the hand-written embed declarations themselves, no central metadata.
C++: the is_base_of family probes at compile time (example); note is_base_of_v<Loggable, Jsonable> checks the inheritance DIRECTION; C++ multiple inheritance makes "parents" possibly many - std::is_base_of verifies pairwise; names are non-portable so everything is boolean.
C#: GetInterfaces() lists all interfaces (including inherited ones), BaseType the single parent (example); note C#'s GetInterfaces differs from Java - it includes the whole inheritance chain; no trait counterpart - mixins are extension methods / default interface methods.
Go: interfaces are satisfied IMPLICITLY - you CANNOT enumerate which interfaces a type implements (example), only assert one by one in reverse; var _ I = T{} is the compile-time assertion idiom; the philosophy is the opposite of the PHP trio's forward enumeration, a core Go trade-off.
Java: getInterfaces() lists DIRECTLY declared interfaces only (the example stresses this) - interfaces Admin inherits via Base must be asked of Base; getSuperclass() is a single chain; loop upward for the full chain.
JavaScript: no interface metadata - the prototype walk (example) yields "parents" only; "interfaces" are simulated duck-typing (checking method presence), instanceof checks chain classes.
PHP: each of the trio returns a name => name map (the example purifies with array_keys); class_parents is the full chain (linear - no multiple inheritance); class_uses holds only DIRECTLY used traits (not the transitive closure of trait-in-trait, which needs recursion); since PHP 8.4 class_uses throws Error on an unknown class instead of returning false.
Python: __mro__ full chain + __bases__ direct parents (example); Python unifies interfaces and parents into one base-class list - mixins ARE Python's interface shape; real interface semantics need ABCs, via ABC.register or __subclasshook__.
Rust: everything at compile time (example) - trait implementation relations have no runtime list; the generic bound require_json(&Admin) IS the interface-check shape; runtime alternatives use dyn Any + downcast, but "which traits" remains unreachable.
TypeScript: interfaces are compile-time erased (example) - only the parent chain survives at runtime; TS-only conditional extends types provide type-level "relation queries": Admin extends Base ? ... : ... is compile-time is_base_of.