PHP Function Reference

PHP enum_exists() Function



The PHP enum_exists() function is used to check whether or not the given enum has been defined. It returns true if the given enum is defined, else returns false.

Note: This function is available as of PHP 8.1.0.

Syntax

enum_exists(enum, autoload)

Parameters

enum Required. Specify the enum name to check. The name is matched in a case-insensitive manner.
autoload Optional. Specify whether to call __autoload or not by default.

Return Value

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

Example: enum_exists() example

The example below shows the usage of enum_exists() function.

<?php
//checking that the enum exists 
//before trying to use it
if (enum_exists(Suit::class)) {
  $myclass = Suit::Diamonds;
  echo "Suit::class enum is defined.";
} else {
  echo "Suit::class is not defined.";
}
?>

The output of the above code will be:

Suit::class is not defined.

❮ PHP Classes/Objects Reference

5