enum_exists
enum_exists(string $enum, bool $autoload = true): bool
Checks whether an enum is defined (may trigger autoloading); the enum sibling of class_exists/interface_exists/trait_exists, completing the PHP 8.1 type-table quartet.
C C23
#include <stdio.h>
/* C enums are compile-time integer constants: there is no runtime
object to query for existence */
enum Suit { HEARTS, SPADES };
int main(void) {
enum Suit s = HEARTS;
printf("%d\n", (int)s); /* exists only as this constant */
return 0;
}
0
C++ C++26
#include <iostream>
#include <type_traits>
enum class Suit { Hearts, Spades };
class Plain {};
int main() {
// the existence question is answered at COMPILE time:
std::cout << std::boolalpha
<< std::is_enum_v<Suit> << "\n"; // true
std::cout << std::is_enum_v<Plain> << "\n"; // false - a class
std::cout << std::is_enum_v<int> << "\n"; // false
return 0;
}
true false false
C# v11.0.100
enum Suit { Hearts, Spades }
class Plain { }
class MainClass {
static void Main() {
System.Console.WriteLine(typeof(Suit).IsEnum); // True
System.Console.WriteLine(typeof(Plain).IsEnum); // False
System.Console.WriteLine(typeof(int).IsEnum); // False
}
}
True False False
Go v1.27.4
package main
import "fmt"
// Go has no enum type: the idiom is typed constants with iota.
// Existence cannot be checked at runtime - a constant either
// compiles or the program does not build.
type Suit int
const (
Hearts Suit = iota
Spades
)
func main() {
fmt.Println(Hearts, Spades) // 0 1
fmt.Println(Suit(7)) // 7 - no validation exists
}
0 1 7
Java v27.0.1
enum Suit { HEARTS, SPADES }
class Plain { }
class Main {
public static void main(String[] args) {
System.out.println(Suit.class.isEnum()); // true
System.out.println(Plain.class.isEnum()); // false
System.out.println(int.class.isEnum()); // false
// the enum-family check via assignability:
System.out.println(Enum.class.isAssignableFrom(Suit.class)); // true
}
}
true false false true
JavaScript v26.13.1
// JS has no enum: the idiom is a frozen object of constants
const Suit = Object.freeze({ Hearts: "H", Spades: "S" });
console.log(Suit.Hearts); // H
console.log(Object.isFrozen(Suit)); // true - the closest "is enum" check
H true
PHP v8.6.0
enum Suit: string { case Hearts = "H"; case Spades = "S"; }
class Plain {}
echo var_export(enum_exists(Suit::class), true), "\n"; // true
echo var_export(enum_exists(Plain::class), true), "\n"; // false: class, not enum
echo var_export(enum_exists(DateTime::class), true), "\n"; // false: built-in class
echo var_export(class_exists(Plain::class), true), "\n"; // the sibling check
true false false true
Python v3.15.2
import enum
class Suit(enum.Enum):
HEARTS = 1
class Plain:
pass
print(issubclass(Suit, enum.Enum)) # True
print(issubclass(Plain, enum.Enum)) # False
print(issubclass(int, enum.Enum)) # False
print(isinstance(Suit.HEARTS, Suit)) # members exist as attributes
True False False True
Rust v1.99.1
// Rust has no runtime type registry: enum existence is decided at
// compile time. A match must cover every variant the compiler knows.
enum Suit {
Hearts,
Spades,
}
fn main() {
let s = Suit::Hearts;
match s {
Suit::Hearts => println!("H"),
Suit::Spades => println!("S"),
}
// Suit::Ghost would simply fail to compile - no runtime check exists
}
H
TypeScript v26.13.1
// modern TS style: a const object + union type stands in for `enum`
// (the enum keyword is non-erasable syntax; at runtime it was always
// just a plain object anyway - this is the same thing, spelled out)
const Suit = {
Hearts: "H",
Spades: "S",
} as const;
type Suit = (typeof Suit)[keyof typeof Suit];
console.log(Suit.Hearts); // H
console.log(typeof Suit); // object
console.log("Hearts" in Suit); // true - member presence check
H object true
Language differences
C: enums are compile-time integer constant sets (example), no runtime object to query; the existence question does not exist in C - an undefined enum name is a compile error.
C++: std::is_enum_v<T> is a compile-time category assertion (example), one step earlier than PHP's runtime existence query - the type system already knows the answer; scoped enum class shares the philosophy of PHP 8.1 backed enums.
C#: Type.IsEnum (example) is the most direct counterpart; note a nullable enum Suit? has IsEnum false (the underlying type is Nullable<T>).
Go: no enum type - const + iota is the idiom (example); Suit(7) validates nothing, neither existence nor legality; write a switch or a map lookup yourself for validation.
Java: Class.isEnum() (example) plus the Enum.class.isAssignableFrom(...) family check; Java enums ship values()/valueOf(), so existence checks usually wrap valueOf against IllegalArgumentException.
JavaScript: no enums - Object.freeze({...}) is the idiom (example), Object.isFrozen the closest "is it an enum" heuristic; a compiled TS enum is exactly this shape (plus reverse mapping).
PHP: enum_exists completes the 8.1 type-table quartet: class_exists/interface_exists/trait_exists/enum_exists (the fourth line contrasts); an enum is NOT a class (class_exists(Suit::class) is false) - "enums are not classes" is the key 8.1 design; the $autoload argument can trigger autoloading.
Python: issubclass(X, enum.Enum) (example); a Python enum is an ordinary class (issubclass true, the opposite of PHP - enum_exists is true but class_exists false there); members are class attributes, Suit.HEARTS reachable directly.
Rust: everything is decided at compile time (exhaustive match in the example); no runtime type table, so existence is a non-question at runtime; crates like strum can derive from_str-style lookups.
TypeScript: the example uses the modern idiom - a const object with as const + a union type instead of the enum keyword, which is non-erasable syntax (native Node type-stripping rejects it outright); a runtime TS enum compiles to a plain object anyway (string enums get no reverse mapping, numeric ones do); the in operator checks members; a const enum inlines at compile time - no object exists at runtime.