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.
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!