Best practice of using PHP Enums!

Mehrab Hojjati Pour
3 min readMay 22, 2022
PHP Enum, best practice of using them

Hi, as you know, PHP enums were introduced with PHP 8.1, so there is a good chance you may not use them until now or perfectly.
Therefore, let’s look at what an enum is and how to use it in PHP.

Although I’m pretty sure that before PHP 8.1, some gurus which fake the enum behavior using PHP classes like below:

class Status {
public const REJECTED = 0;
public const PENDING = 1;
public const ACCEPTED = 2;
}

This work’s well enough, but in my opinion, it’s not powerful enough.

But wait! why would you even need these enums or classes to fake something called enums? What’s the purpose of this functionality?

Storing states/statuses in your database without a reference point of potentials isn’t ideal.
Ideally, you’d want to see ALL the states an article could have.

Article::create([
"title"=> "Hello world enums!",
"status"=> "pending"
]);

So I guess you all can see the problems behind this, right?

But here’s an enum.

enum Status: int {
case REJECTED = 0;
case PENDING = 1;
case ACCEPTED = 2;
}

In this example, we pre-defined all the statuses an article could have.
This is a ‘backed enum’ that holds values for each case. You can hint for either int or string only, and can’t mix types.

Let’s jump straight to how you’d use this. Let’s create an article with a status taken directly from the “PENDING” case. The “->value” here is a property on the Enum object (Enums are objects).

Article::create([
"title"=> "Hello world enums!",
"status"=> Status::PENDING->value // returns 1
]);

In our example, we’ve now got the value ‘1’ stored in the database, representing “STATUS”.

How do we resolve the Enum back from this value? Like this:

$article = Article::first();// this returns an enum object back
$status = Status::from($article->status);

What’s the point of getting an Enum back from a case value?

Well, Enums can have methods, so we could create a method to get the status to show in the UI, for example.

(I had to use an image to fit all codes perfectly)

You can use this “presentTense ” method by calling it directly on the resolved Enum.
So, if you were listing through a user’s article, you’d be able to output a custom status next to the article.

$article = Article::first();// this returns an enum object back
$status = Status::from($article->status)->presentTense();

If you want to output all possible article status options on a form in your UI, how do you get all possible Enum values?

Enums have a method that’ll return them for you!
You could use this to build up a select element with options, ready to submit through and store.

If you don’t know what is @foreach or why I’ve used curly brackets to show the value of status, I highly suggest taking a look at the Laravel framework.

Thanks for your time; I hope this post was helpful for you.

--

--

Mehrab Hojjati Pour

a full-stack developer with more focused on Back-end with more than 7 years of experience. love to create new things and grow startups