PHP.nl

BackedEnum::tryFrom

BackedEnum::tryFrom

Maps a scalar to an enum instance or null

 **BackedEnum::tryFrom**  $value

The method translates a or into the corresponding Enum case, if any. If there is no matching case defined, it will return null. tryFrom``string``int

valueThe scalar value to map to an enum case.

A case instance of this enumeration, or if not found. null

Voorbeeld: Basic usage

The following example illustrates how enum cases are returned.

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

$h = Suit::tryFrom('H');

var_dump($h);

$b = Suit::tryFrom('B') ?? Suit::Spades;

var_dump($b);
?>
enum(Suit::Hearts)
enum(Suit::Spades)

UnitEnum::cases``BackedEnum::from

Documentatie