class_exists
class_exists(string $class, bool $autoload = true): bool
Checks whether a class is defined (or can be autoloaded); part of the family with interface_exists/enum_exists.
C C23
#include <stdio.h>
/* C has no class concept - the nearest analogue is whether a type/symbol is
declared at compile time; sizeof proves the type is known */
struct Suit {
int rank;
};
int main(void) {
printf("%zu\n", sizeof(struct Suit)); /* compile-time existence proof */
struct Suit s = {1};
printf("%d\n", s.rank == 1 ? 1 : 0);
return 0;
}
4 1
C++ C++26
#include <iostream>
#include <type_traits>
class Suit {};
int main() {
// C++ binds at compile time; "type existence" becomes a template question.
// trait detection is the compile-time probe (class_exists analogue)
std::cout << std::boolalpha;
std::cout << std::is_class_v<Suit> << "\n";
std::cout << std::is_class_v<int> << "\n";
return 0;
}
true false
C# v11.0.100
// Type.GetType resolves by name in loaded assemblies; throwOnError:false
// turns "missing" into null (PHP's false)
Console.WriteLine(Type.GetType("System.String") != null);
Console.WriteLine(Type.GetType("NoSuch.Class.A20", throwOnError: false) == null);
// the interface family (interface_exists analogue)
Console.WriteLine(typeof(System.Collections.IEnumerable).IsInterface);
True True True
Go v1.27.4
package main
import "fmt"
type Suit int
func main() {
// Go resolves every identifier at compile time; "does this type exist" is
// not a runtime question - the registry-map pattern is the runtime analogue
registry := map[string]any{"Suit": Suit(0)}
_, ok := registry["Suit"]
fmt.Println(ok)
_, ok = registry["NoSuchClassA20"]
fmt.Println(ok)
}
true false
Java v27.0.1
// Class.forName probes the classpath; a missing class throws
// ClassNotFoundException - try/catch turns it into a boolean (PHP's false)
class Main {
public static void main(String[] args) {
System.out.println(exists("java.util.Map"));
System.out.println(exists("java.util.NoSuchClassA20"));
}
static boolean exists(String name) {
try {
Class.forName(name);
return true;
} catch (ClassNotFoundException e) {
return false;
}
}
}
true false
JavaScript v26.13.1
// JS has no class table; typeof X !== "undefined" probes runtime
// existence without throwing on undeclared identifiers
console.log(typeof Map !== "undefined");
console.log(typeof NoSuchClassA20 !== "undefined");
class Suit {}
console.log(typeof Suit !== "undefined");
true false true
PHP v8.6.0
// class_exists is the class family probe; the second parameter decides
// whether the autoload queue fires (pairs with spl_autoload_register)
enum Suit: string { case Hearts = "H"; }
var_dump(class_exists("stdClass")); // built-in class
var_dump(class_exists("NoSuchClassA20")); // missing, autoloader finds nothing
var_dump(interface_exists("Traversable")); // interface family
var_dump(enum_exists("Suit")); // enum family (8.1+)
var_dump(class_exists("Traversable")); // interfaces are not classes
bool(true) bool(false) bool(true) bool(true) bool(false)
Python v3.15.2
import importlib.util
import json
# importlib.util.find_spec locates a module WITHOUT importing it - the
# autoload=false analogue; hasattr checks members after import
def module_exists(name):
spec = importlib.util.find_spec(name)
return spec is not None
print(module_exists("json"))
print(module_exists("no_such_module_a20"))
print("JSONDecoder" in dir(json))
True False True
Rust v1.99.1
// Rust binds everything at compile time; existence probing is a trait
// question, not a runtime lookup. A registry shows the runtime shape:
use std::collections::HashMap;
fn main() {
let mut registry: HashMap<&str, fn() -> &'static str> = HashMap::new();
registry.insert("Suit", || "hearts");
println!("{}", registry.contains_key("Suit"));
println!("{}", registry.contains_key("NoSuchClassA20"));
}
true false
TypeScript v26.13.1
// Same runtime probe as JS; declared-but-missing names are caught at
// compile time, so runtime probing targets only dynamic keys
console.log(typeof Map !== "undefined");
const g = globalThis as Record<string, unknown>;
console.log(typeof g["NoSuchClassA20"] !== "undefined");
class Suit {}
console.log(typeof Suit !== "undefined");
true false true
Language differences
C: no runtime type query — referencing an undefined type is a compile error; #ifdef/preprocessor macros are the only conditional-existence mechanism, and runtime dynamic probing is symbol-level dlsym only.
C++: compile-time binding; type probing happens in template metaprogramming (std::is_class_v and other traits, example) — a different dimension from PHP's runtime table lookup.
C#: Type.GetType with throwOnError:false (example); note it searches loaded assemblies only — cross-assembly needs Assembly.GetType, which parallels PHP's autoload=true triggering the loader.
Go: every identifier resolves at compile time; "does the type exist" is not a runtime question (the registry emulation is honest, example); feature probing uses build tags, also compile-time.
Java: Class.forName searches the current classloader's classpath and throws when missing (example converts to boolean); interfaces checked via clazz.isInterface() — same split as PHP's interface_exists.
JavaScript: typeof is the only probe that does not throw on undeclared identifiers (example); class declarations are block bindings, so closure/module-scope classes are invisible — no global class table at the language level.
PHP: the autoload interaction is the core — the second parameter allow_autoload defaults to true, so probing a missing class fires the queue registered by spl_autoload_register (which may make the class "exist"); pass false to check only defined classes; the family: class_exists/interface_exists/enum_exists — interfaces and enums are not classes (last line of the example).
Python: module-level probing uses importlib.util.find_spec (no import executed, the autoload=false analogue); class/object level uses hasattr; no dedicated enum-existence function — enums are plain object attributes.
Rust: compile-time binding, a missing type is a compile error; existence is expressed at the trait/generic-constraint level (specialization-probe idioms); the runtime registry here is only illustrative.
TypeScript: compile time rejects undefined names outright (typeof matters only for dynamic keys, example); keyof typeof / conditional types probe at the type level.