This episode is for members only

Sign up to access "PHP Enums" right now.

Get started
Already a member? Sign in to continue
Playing
02. Defining enums

Transcript

00:00
Let's look at this syntax then to build out our first enum.
00:03
So I'm going to go ahead and call this book status. We prefix this with enum here. And it kind of looks like a class in the sense that we define it like this. Instead of defining constants though, we use cases.
00:14
So I'm going to go ahead and say want to read. And let's define out all of these here. Reading. And we'll have a case here of read.
00:23
So we've now got three potential statuses for this enum. Now, this is called a pure enum because it doesn't have any values associated with each case. These can be useful, but we're going to be looking at backed enums later which contain
00:37
values for these. And they tend to be a lot more useful when you're storing in the database. So the first thing that we're going to do is just go ahead and var dump out this enum and access, say, want to read.
00:48
So we're going to go ahead and var dump out status. We still use the scope resolution operator here. And then we just go ahead and give the value in here, almost like it's a constant on this class.
00:58
So let's just take a look at what this gives us. And you can see that the value that we get back is actually an enum object. And enums are objects. And they're actually behind the scenes implemented like singletons.
01:09
Just to kind of prove this, let's go ahead and say is object within PHP. And of course, we'd imagine we would get true out here because I've just told you that they're objects. Now, before we dive into looking at backed enums, the really good thing about enums is
01:22
that now we have a type that we can hint. So for example, let's just say that we had a model in your favorite framework. And you had a method in here that filled in the details about what you wanted to store. Let's just say that that took in a title.
01:37
And we want to take in a status. Now, if we were using just pure integers or we were using strings, we would have to type in this as a string. So that would be a status.
01:47
What we can now do, though, is make sure that we get back a book status or we receive in a book status value inside of here. So if we just var dump out on this, or we don't even have to do that, we could just go ahead and new this up.
02:01
So let's new up a book in here. And let's go ahead and call book and fill and just type in a book. And now what we can do is say book status and reading, for example. Let's go ahead and just var dump that book just to see what we've got inside of there.
02:18
And just before we dump that out, let's actually go ahead and, in fact, dump this directly from in here. So let's say title and status. And let's make sure that's var dump.
02:29
And let's go down and just invoke fill. And that should dump that out for us. There we go. So we've got an enum in there and a string.
02:36
Now, if we were to try and just pass a string directly into here, of course, that's not going to work because this is type hinted to book status. So this will give the developer a hint that they actually need to pass in a book status as part of the predefined statuses that we have here.
02:51
So that's why enums are helpful. We've learned how to define these out like this. But we're going to go ahead over in the next episode and look at backed enums, which are going to be a lot more interesting and a lot easier to fill directly into our database.

Episode summary

In this episode, we dive into how to define enums in PHP from scratch. We kick things off by creating our own enum called BookStatus, which represents different reading states for a book like WantToRead, Reading, and Read. You'll see how the enum syntax is similar to a class but instead of constants, we define "cases."

We experiment a bit by dumping out the enum values to the screen and see that each one is actually an object—a neat detail about enums in PHP! There’s a quick detour to confirm this with a classic is_object check. The video also highlights a major benefit: enums give us a dedicated type that we can use for type-hinting in functions or methods. This makes our code less error-prone because you can’t accidentally pass in just any value—it has to be one of the enum's predefined statuses.

To wrap up, we try passing the enum through a mock fill method (like you’d see in a framework model). This shows off how using enums makes your code more robust, since you get a type error if you try to pass a plain string instead of an enum case. Next time, we’ll check out backed enums, which take things further and are even more useful, especially for storing values in a database.

Episode discussion

No comments, yet. Be the first!