murilo's avatar
Level 10

Enum vs Utilities, best praticle if is not using database

in laravel , Does enuns should be used just when you use database enum ? for example - I have a list of actions that I am passing in a form , if I am not using this in database . I could as well make a utility .

I created a enum , just for a example -

enum FormAction: string
{
    case Create = 'create';
    case Update = 'update';
    case Delete = 'delete';
}


I could make it as a utility as well, instead -

class FormActions
{
    public const CREATE = 'create';
    public const UPDATE = 'update';
    public const DELETE = 'delete';

    public static function all(): array
    {
        return [self::CREATE, self::UPDATE, self::DELETE];
    }
}

if I am not using that in database , what wold be the best praticle , use the ENUM or use UTILITY ?

0 likes
4 replies
LaryAI's avatar
Level 58

In Laravel, the choice between using an enum and a utility class for managing a set of constants depends on your specific use case and preferences. Here are some considerations to help you decide:

Using Enums

  1. Type Safety: Enums provide type safety, which means you can ensure that only valid values are used. This can help prevent bugs and make your code more robust.

  2. Readability: Enums can make your code more readable and self-documenting. When you see FormAction::Create, it's clear that this is a specific action type.

  3. Method Support: Enums can have methods, which allows you to encapsulate related logic within the enum itself.

  4. Switch Statements: Enums can be used in switch statements, which can be more readable than a series of if-else statements.

  5. PHP 8.1+: Enums are a feature introduced in PHP 8.1, so if you're using an older version of PHP, you won't be able to use them.

Using Utility Classes

  1. Simplicity: Utility classes are simple and straightforward. They are just a collection of constants, which can be easier to understand for developers who are not familiar with enums.

  2. PHP Version: If you're using a version of PHP older than 8.1, utility classes are your only option.

  3. Flexibility: Utility classes can be more flexible if you need to add additional methods or logic that don't fit well within an enum.

  4. No Type Safety: Unlike enums, utility classes don't provide type safety, so you need to be more careful to ensure that only valid values are used.

Conclusion

If you're using PHP 8.1 or later and you value type safety and readability, using an enum is generally the better choice. It provides a more structured and self-documenting way to handle a fixed set of related constants.

However, if you're on an older version of PHP or prefer a simpler approach, a utility class is perfectly acceptable. It ultimately comes down to your specific needs and the context of your application.

Here's a quick summary:

  • Use Enums if you want type safety, better readability, and are using PHP 8.1+.
  • Use Utility Classes if you need simplicity, flexibility, or are on an older PHP version.

Both approaches are valid, so choose the one that best fits your project's requirements and your team's familiarity with the concepts.

aleahy's avatar

If you can use enums in your project, I don't see the point of the utility classes. If you're not going to use it in any sort of database, you don't even need to define the string.

The only difference I can think of is that a class can be extended with a subclass, but an enum cannot. It can only implement interfaces. But you shouldn't extend it anyway, if it means separating your definition of constants in different files for the same class.

1 like
Snapey's avatar

best advice is to forget database enums even exist, and use php enum class

1 like
martinbean's avatar

@murilo Enums are just a data type. All they are, is a list of possible values for something. So if you had for example an order model, and that model had a status, you’d define an enum that contained all the available order statuses:

enum OrderStatus
{
    case Open = 'open';
    case Submitted = 'submitted';
    case Processing = 'processing';
    case Complete = 'complete';
    case Cancelled = 'cancelled';
}

When it comes to the database, as @snapey suggests, forget they exist. Just use a string-like column to hold the value:

$table->string('status');

The reason being, if you need to add, change, or remove a case from the enum, then you can’t just change the column like you can other column types. You have to physically delete and re-create the column anew each time you wish to change an option. So, to avoid that, just store enum values as strings in the database, but use enums in code to enforce domain logic.

Please or to participate in this conversation.