RomkaLTU
RomkaLTU

Reputation: 4129

Enums extend or use traits (reusability)

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.

Upvotes: 4

Views: 9494

Answers (1)

Riccardo Venturini
Riccardo Venturini

Reputation: 478

Enum cannot be extended, and must not inherit

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

Upvotes: 9

Related Questions