F
Filament8mo ago
Shavik

PHP Enum "Inheritance " in Filament

First off, I know you can't extend an enum in PHP, I'm trying to figure a way to accomplush that functionality though for the Filament getLabel, etc methods. Lets say I have a base "Laravel" package that has among other things, an enum. It's a plain 'ol, string backed enum. Nothing fancy. If I then have a Filament plugin (package) that wants to use that enum, PHP lacking enum inheritance strikes again. Does anyone have any preferred 'tricks' for 'extending (but not really)' an enum in this Filament package so that it has the getLabel, getColor, etc methods? For example, the base Laravel package would have no Filament dependencies. It could have an enum like
enum BaseStatus: string {
case Success = 'success';
}
enum BaseStatus: string {
case Success = 'success';
}
and in the Filament plugin that uses it, I'd like to have some way to 'add' the getLabel, etc to it. In a dream world:
enum Status: string extends BaseStatus {
// Cases are carried over from the base
public function getLabel() {
// ...
}
}
enum Status: string extends BaseStatus {
// Cases are carried over from the base
public function getLabel() {
// ...
}
}
Been scratching my head at a clean way to solve this for a day or two. Just didn't know if anyone else had encountered this or knew of a good way to handle it. Thanks!
4 Replies
Dan Harrin
Dan Harrin8mo ago
trait! enums cannot inherit in php
Lara Zeus
Lara Zeus8mo ago
not sure if you can bind enums to laravel container
$this->app->bind(
packageEnum::class,
filamentEnum::class
);
$this->app->bind(
packageEnum::class,
filamentEnum::class
);
Shavik
Shavik8mo ago
That's an interesting approach that I hadn't considered. Well effectively the child package would need to 'inject' the trait into the base package's enum? Because it'd be the child package that is 'defining' labels and colors via the Filament contract/methods.
Dennis Koch
Dennis Koch8mo ago
But that will only work in places where the enum is resolved via container