Skip to content

Commit

Permalink
support inhertance
Browse files Browse the repository at this point in the history
  • Loading branch information
miladrahimi committed Jul 17, 2021
1 parent bcb65a2 commit 7f5fe89
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/Enum.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace MiladRahimi\Enum;

use ReflectionClass;
use ReflectionException;
use Throwable;

abstract class Enum
{
Expand All @@ -22,8 +22,12 @@ abstract class Enum
public static function all(): array
{
try {
return static::$items ?: static::$items = (new ReflectionClass(static::class))->getConstants();
} catch (ReflectionException $e) {
if (isset(static::$items[static::class])) {
return static::$items[static::class];
}

return static::$items[static::class] = (new ReflectionClass(static::class))->getConstants();
} catch (Throwable $e) {
return [];
}
}
Expand Down Expand Up @@ -130,4 +134,4 @@ public static function randomValue()
{
return static::all()[array_rand(static::all())];
}
}
}

0 comments on commit 7f5fe89

Please sign in to comment.