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.
7 episodes 31 mins

Overview

Enums represent a typed set of possible values in PHP. For example, if you're storing a Book that can have a status of want to read, reading or read, an Enum will hold these values in one place that you can reference anywhere.

Prior to PHP 8.1 (when Enums were added), you'd have to work with a simple class with constants. While that works fine, it doesn't provide much power under the hood.

In this course, we'll look at the power of Enums and how they can drastically simplify your code.

Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!

Episode discussion

No comments, yet. Be the first!