array ( 0 => 'index.php', 1 => 'PHP Manual', ), 'head' => array ( 0 => 'UTF-8', 1 => 'en', ), 'this' => array ( 0 => 'backedenum.from.php', 1 => 'BackedEnum::from', ), 'up' => array ( 0 => 'class.backedenum.php', 1 => 'BackedEnum', ), 'prev' => array ( 0 => 'class.backedenum.php', 1 => 'BackedEnum', ), 'next' => array ( 0 => 'backedenum.tryfrom.php', 1 => 'BackedEnum::tryFrom', ), 'alternatives' => array ( ), 'source' => array ( 'lang' => 'en', 'path' => 'language/predefined/backedenum/from.xml', ), ); $setup["toc"] = $TOC; $setup["toc_deprecated"] = $TOC_DEPRECATED; $setup["parents"] = $PARENTS; manual_setup($setup); ?>
(PHP 8 >= 8.1.0)
BackedEnum::from — Maps a scalar to an enum instance
The from() method translates a string or int into the corresponding Enum case, if any. If there is no matching case defined, it will throw a ValueError.
value
The scalar value to map to an enum case.
A case instance of this enumeration.
Example #1 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::from('H');
var_dump($h);
$b = Suit::from('B');
?>
The above example will output:
enum(Suit::Hearts) Fatal error: Uncaught ValueError: "B" is not a valid backing value for enum "Suit" in /file.php:15