xref: /PHP-8.1/Zend/tests/enum/backed-tryFrom.phpt (revision 269c8dac)
1--TEST--
2BackedEnum::tryFrom()
3--FILE--
4<?php
5
6enum Suit: string {
7    case Hearts = 'H';
8    case Diamonds = 'D';
9    case Clubs = 'C';
10    case Spades = 'S';
11}
12
13var_dump(Suit::tryFrom('H'));
14var_dump(Suit::tryFrom('D'));
15var_dump(Suit::tryFrom('C'));
16var_dump(Suit::tryFrom('S'));
17var_dump(Suit::tryFrom('X'));
18
19enum Foo: int {
20    case Bar = 1;
21    case Baz = 2;
22    case Beep = 3;
23}
24
25var_dump(Foo::tryFrom(1));
26var_dump(Foo::tryFrom(2));
27var_dump(Foo::tryFrom(3));
28var_dump(Foo::tryFrom(4));
29
30?>
31--EXPECT--
32enum(Suit::Hearts)
33enum(Suit::Diamonds)
34enum(Suit::Clubs)
35enum(Suit::Spades)
36NULL
37enum(Foo::Bar)
38enum(Foo::Baz)
39enum(Foo::Beep)
40NULL
41