Home > Back-end >  Enums extend or use traits (reusability)
Enums extend or use traits (reusability)

Time:04-15

I have enums that must have additional methods for translation purposes:

<?php

declare(strict_types=1);

namespace App\Enums;

enum GenderEnum: string
{
    case MALE = 'male';
    case FEMALE = 'female';

    public function trans(): string
    {
        return trans('enums.' . $this->value);
    }
}

This method is trans and it will be duplicated in all enums, how can I avoid duplication? I can't extend it using traits in enums.

CodePudding user response:

Enum cannot be extended, and must not inherit

but you can use Traits, as long as the trait does not declare any properties

  • Related