跳转到主要内容

tryfrom

BackedEnum::tryFrom

Maps a scalar to an enum instance or null

ENTITY

public

static

static

null

BackedEnum::tryFrom

int

string

value

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

ENTITY

value
The scalar value to map to an enum case.

ENTITY

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

ENTITY

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);
?>

   

ENTITY

enum(Suit::Hearts)
enum(Suit::Spades)

ENTITY

UnitEnum::cases, BackedEnum::from