This episode is for members only

Sign up to access "PHP Enums" right now.

Get started
Already a member? Sign in to continue
Playing
03. Backed enums

Transcript

00:00
We've looked at this pure enum, but now we're going to take a look at backed enums, which
00:04
are going to be a lot more useful for storing values directly in the database. These do have their place, but most of the time you'll find yourself using backed enums.
00:12
So we'll start with this example that we've already got. Let's assign an integer value to each of these, much like we did with the class earlier. We had these public constants that we were using to dump.
00:23
Now notice my editor just sort of looks like it's erroring at the moment. If we just go ahead and try and var dump out one of these, let's just see what error we get.
00:32
So I'm going to go ahead and say book status, and let's choose reading. So that should give us a value of 2 back in our terminal. So let's run this.
00:40
You can see here that case want to read of non-backed enum book status must not have a value. Try adding here now. That might seem a little bit confusing if you're new.
00:49
Basically, when we assign values to enums, we have to define the type of these in here for it to work nicely. So now, if we go ahead and try and run this,
00:59
you can see that we get enum book status reading. Now, we still don't get the value. We're going to look at that in just a second. But let's just look at what happens
01:07
if we wanted to change this over to a string. So want to read and reading and read. Now, of course, we're going to have to change the type hint here because these are not integers.
01:17
So let's switch this over to a string. And let's go ahead and run this again. And you can see that that still works. Now, the only two types that we can have are string and integer.
01:26
And we can't mix them. So you couldn't, for example, do this and have this as int or string. It just wouldn't work.
01:33
We have to choose a single, either a string or an integer for each of our items or cases within our backed enums. OK, let's leave these as strings. And let's take a look at how we can grab the value from here.
01:46
Because if you wanted to store this in your database, obviously, the entire object itself, you wouldn't want to go ahead and put in. So to do this, what we do is we access the value as normal.
01:57
So reading, want to read, whatever we want to do. And then we go ahead and grab the value like this. Remember, this is an object. So we can access this property.
02:06
Because this technically returns the enum in the sense that we've got the reading enum, we've seen that just here, that means that the value, when we access it, is going to be the value that we've assigned to this case
02:18
that we've grabbed. So let's go ahead and just run this. And sure enough, we get reading. We can change this around to something else like want to read.
02:25
And of course, it's going to work in exactly the same way and just return to us the different string or integer, depending on what we've chosen. So you can now use this to persist it in the database.
02:34
Let's just bring back that example of our list of attributes that we want to save in the database. We'll go ahead and give this a title really quickly, so a book. And the status now, we know we can access directly
02:46
from that enum. It's all in one place. If it changes, it's really convenient. So we can go ahead and pull this, pop that there.
02:53
And then we can go ahead and var dump on them attributes. And we end up with the following. So a book, and then the status as want to read, either a string or an integer for this.
03:03
So this is how you would use an enum to define out these cases, a backed enum with values, and then access the value when you're storing it in the database.
03:12
So since we're technically discussing serialization here, let's go over to the next episode and have a little bit of a deeper dive into serialization and deserialization with enums.

Episode summary

In this episode, we're diving into backed enums and how they're actually a lot more useful than pure enums when it comes to things like storing values directly in a database.

We kick off by taking our existing example and modifying our enum cases so that each one has an explicit value, just like we did with constants earlier in the course. The video walks you through a little "gotcha"—if you assign a value to an enum case you have to explicitly define the type that enum is "backed" by (either int or string). The editor will actually throw errors if you forget to do this, so you'll see exactly what goes wrong and how to fix it.

You'll see that with backed enums, all the values have to be the same type—either integers or strings, but not both. So, you can't mix and match in one enum.

From there, I show you how to access the actual value for each enum case (super important if you're persisting these values in your database!), and how you can use these values just like you would any string or integer elsewhere in your code.

We end off by quickly showing how to bundle up attributes from your enum when saving things like a book's status, and how convenient it is to have these all defined in a single place. This sets us up nicely for the next episode, where we'll get into serialization and deserialization with enums.

So by the end of this one, you'll know how and why to use backed enums, and how to wrangle the values out of them for saving in your database!

Episode discussion

No comments, yet. Be the first!