Skip to content

Instantly share code, notes, and snippets.

@phuclh
Last active September 17, 2021 21:11
Show Gist options
  • Save phuclh/17f693d8b6ee3c4b5671fb93366c15e9 to your computer and use it in GitHub Desktop.
Save phuclh/17f693d8b6ee3c4b5671fb93366c15e9 to your computer and use it in GitHub Desktop.
abstract class BaseEnum
{
protected static array $options = [];
public static function options(): array
{
if (count(static::$options)) {
return static::$options;
}
foreach ((new ReflectionClass(new static))->getConstants() as $constant) {
static::$options[$constant] = Str::title(str_replace('-', ' ', $constant));
}
return static::$options;
}
}
@phuclh
Copy link
Author

phuclh commented Sep 17, 2021

Usage:

class Status extends BaseEnum
{
    const ACTIVE = 'active';
    const PENDING = 'pending';
}
Status::ACTIVE;
Status::PENDING;

Status::options();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment