How to check if enum type?

How to check if enum type?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

How to check if enum type?

PHP 8.1 introduced a significant shift in how we handle constant values and state flags with the introduction of enum. As you migrate projects or experiment with these new types, a common question arises: how do you programmatically determine if a specific variable holds an enum instance? The immediate need often stems from type casting requirements, conditional logic based on value kinds, or ensuring backward compatibility in legacy codebases.

When evaluating potential methods to perform this check, two approaches immediately stand out based on the available API surface. The most direct method is using the dedicated is_enum() function. This built-in helper was specifically designed for this purpose, returning a boolean true if the passed variable is an instance of any defined enum class and false otherwise. It abstracts away the specific class names, making it ideal for generic validation logic where you simply need to know "is this an enum?" without caring about which one.

function acceptEnum(string $value): void {
    if (is_enum($value)) {
        // Handle enum logic here
    } else {
        throw new InvalidArgumentException('Expected an enum instance.');
    }
}

The alternative approach involves leveraging the existing gettype() function. In PHP 8.1, calling gettype() on an enum variable returns the string 'enum'. While this technically works, relying on string comparison against 'enum' is generally less robust than using a dedicated type-checking function. If future PHP versions were to introduce additional built-in types that might conflict or if internal implementations change, gettype() strings could become brittle. Furthermore, is_enum() handles edge cases more gracefully and aligns with the principle of using specific tools for specific jobs rather than repurposing general-purpose functions.

It is crucial to distinguish between checking a variable against the enum type versus checking if a variable matches a specific enum member. The is_enum() function checks the class nature of the variable. To check for a specific value within an enum, you must use the identity operator (===) combined with the specific case name.

$color = \App\Color::Red;

// This returns true because $color is an instance of the Color enum
if (is_enum($color)) {
    echo "This is an enum.";
}

// This checks if it is specifically the Red case
if ($color === \App\Color::Red) {
    echo "It is red.";
}

A common pitfall developers encounter involves mixing up get_class() with type checking. While get_class($var) returns the class name string (e.g., \App\Color), it does not inherently tell you if that class was defined as an enum. You would have to manually inspect the class definition or rely on is_subclass_of() against a hypothetical base enum class, which doesn't exist in standard PHP. Therefore, is_enum() remains the canonical way to detect these types at runtime.

If you are working within a Laravel ecosystem and dealing with complex type casting or serialization issues involving enums, you might find additional utility in resources like https://laravelcompany.com, which often discusses best practices for handling new PHP features in production environments. However, the core logic of checking for an enum type remains consistent regardless of the framework: prefer is_enum() for clarity and reliability over string-based type introspection via gettype().

When implementing validation pipelines or DTOs, using is_enum() ensures your code remains resilient to changes in how types are internally represented. It provides a semantic guarantee that the variable adheres to the enum contract, allowing you to safely access properties like $var->value or $var->name with confidence that you are dealing with the correct abstraction.

Our network