php8.5
Home/ Manual/ reflection / reflectionenum/ ReflectionEnum::getBackingType

ReflectionEnum::getBackingType

PHP function Edit on GitHub ✎

(PHP 8 >= 8.1.0)

Gets the backing type of an Enum, if any

Description

ReflectionEnum::getBackingType(): ReflectionNamedType|null

If the enumeration is a Backed Enum, this method will return an instance of ReflectionType for the backing type of the Enum. If it is not a Backed Enum, it will return null.

Parameters Parameters

Return Values

An instance of ReflectionNamedType, or null if the Enum has no backing type.

Changelog

VersionDescription
8.2.0The return type is now declared as ?ReflectionNamedType. Previously, ?ReflectionType was declared.

Examples

ReflectionEnum::getBackingType example

php
<?php
enum Suit: string
{
    case Hearts = 'H';
    case Diamonds = 'D';
    case Clubs = 'C';
    case Spades = 'S';
}

$rEnum = new ReflectionEnum(Suit::class);

$rBackingType = $rEnum->getBackingType();

var_dump((string) $rBackingType);
?>

The above example will output:

output
string(6) "string"

See Also

Source: reference/reflection/reflectionenum/getbackingtype.xml · from the official PHP manual (php/doc-en)