marc-mabe / php-enum

Simple and fast implementation of enumerations with native PHP
BSD 3-Clause "New" or "Revised" License
464 stars 36 forks source link

How to return the Enum instance #85

Closed eavilesmejia closed 7 years ago

eavilesmejia commented 7 years ago

Having the class:

class ActionType extends Enum
{
    const FOO = 'foo';
    const BAR = 'bar';
}

How to get the class instance when we pass an undefined value ?

$enumClass = ActionType::class;
$value = 'not_defined_value'

try {
   $enumObject = $enumClass::byValue($value);
} catch( InvalidArgumentException $e) {
  // how to get the ActionType instance ???
  $enumObject = new $enumClass(); 
}
marc-mabe commented 7 years ago

@eavilesmejia What is the enum instance? An enum instance is one of the defined enumerators. So you have to specify which one you wanna have or implement logic to use a default one.

eavilesmejia commented 7 years ago

@marc-mabe thanks, I think is good enough implement a default one.