php8.5
Home/ Manual/ classobj / functions/ enum_exists

enum_exists

PHP function Edit on GitHub ✎

(PHP 8 >= 8.1.0)

Checks if the enum has been defined

Description

enum_exists(string $enum, bool $autoload = true): bool

This function checks whether or not the given enum has been defined.

Parameters

enum

The enum name. The name is matched in a case-insensitive manner.

autoload

Whether to autoload if not already loaded.

Return Values

Returns true if enum is a defined enum, false otherwise.

Examples

enum_exists() example

php
<?php
// Check that the enum exists before trying to use it
if (enum_exists(Suit::class)) {
    $myclass = Suit::Hearts;
}
?>

See Also

Source: reference/classobj/functions/enum-exists.xml · from the official PHP manual (php/doc-en)