Playing
01. Before enums

Transcript

00:00
In this course, we're covering everything you need to know about enums in PHP. Now, if you've never heard of enums before, that's absolutely fine. We're going to take the first couple of parts to explore exactly what they are and why they're useful.
00:12
And if you already know what enums are and you just want to find out a little bit more information about how to use them really effectively within PHP, then this course is going to cover that for you as well. So just a little bit of background, enums haven't always been around in PHP.
00:27
They were introduced in PHP 8.1, and they're essentially a collection of constant values. Now, if you're very new to enums, that might not make any sense at the moment. That's absolutely fine. Let's take a look at what we would do if we wanted to store, say, a book in a database
00:44
with some sort of status. So I'm going to go ahead and create out some sort of attributes array here. And if we were storing a book, we'd probably have a title in here. So I'll just say book title.
00:55
And maybe we wanted some sort of status in here, whether we were reading it, wanting to read it, or had already read it. So that would look like something like reading and want to read. And we'd maybe have read for if we'd read it.
01:11
So usually when we're storing these kind of pieces of data in the database, we would just go ahead and give this a value like this, just a standard string value. And if this was want to read, we would probably underscore it. And that's absolutely fine.
01:24
And then on the UI, when you were showing this book, you could take this value and transform it into something that a user could properly read. But the only problem with this is we don't really know what other statuses a book could have. We know that we've stored this as reading.
01:39
We could, of course, iterate through a list of things that a user could pick from. But what if we wanted to go to a single location to see all of the possible statuses a book could have? And let's not get too hung up on the idea of a book.
01:54
This could be absolutely anything. So anything that contains something like a status or some sort of state needs this kind of functionality. So to solve this kind of issue prior to PHP 8.1 when enums were introduced,
02:11
we would probably include some sort of class like status or book status. That would probably be more appropriate. And then in here, we would create out public constants that we could read from. So we might have a reading constant in here.
02:25
And we would assign that a value like reading. And then we would have the same thing for perhaps want to read. So we would do this. And we would assign that to want to read.
02:37
Now, the difference that makes is if you've got a class somewhere that contains these, you've now got a reference to all of the possible statuses that a book could have. And how would you reference that within this area just here? Well, you would just go ahead and choose book status.
02:52
And you would choose reading, for example. And that would return to the string reading. Sometimes you might even have these as numbers, so 1 and 2. And at the database level, you wouldn't really care about having 1 and 2 in there.
03:04
Because when you retrieve these from the database, you could map them back into this to get some sort of status out. Now, at the moment, that's pretty tricky with a class like this. But generally, this is a much better option even if you're not using enums.
03:19
It just makes a lot more sense to have one location for all of the statuses that you want to read. So for example, if we just go ahead and var dump out these attributes, let's just take a look at what this gives us.
03:30
If we just run this, you can see here that we've got a status of 1. And of course, if that was a status of reading and want to read, then we would get the string back in there that would go ahead and be dumped or saved into the database.
03:45
Now, at the moment, this is a better solution than just storing the plain value in here without really having a reference back to other potential values. But it's still just a class with constants. And it's not very powerful unless we start adding additional functionality to this.
04:02
Now, enums deal with this specific problem and make it a lot easier and a lot more powerful for you to work with this kind of structure. So let's go to the next episode and take a look at building out our first enum using this same example.
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!