This episode is for members only

Sign up to access "PHP Enums" right now.

Get started
Already a member? Sign in to continue
Playing
06. Enum interfaces

Transcript

00:00
So now that we know how to serialize, deserialize enums, create enums in the first place, and all the kind of intricacies around them, let's take a look at a different example. And we're going to look at implementing interfaces for enums, which we can do.
00:15
So we're going to look at a different example from the book Status, and we're going to see why using interfaces could be useful. And this is going to further drill in why enums are a really, really good option for statuses.
00:27
So let's imagine that we were creating an app where we were uploading videos like YouTube. We might have an enum in here that had some sort of visibility status. So that would be whether something is public, private,
00:38
it doesn't really matter. So let's go ahead and define this as an integer, and let's create out a private case in here, and let's create out a public case in here.
00:47
So a video can be private, and a video can be public. Now that's the video itself, but what happens when we're uploading a video? Well, we might have some sort of encoding status that we can show to the user, whether it's currently waiting on being encoded, or whether it's failed,
01:05
or whether it was successful. Well, we might have a failure in here. It's one. We might have a success in here.
01:13
Let's just make sure we capitalize that. And we might have a waiting status. Now the point of this is these two things have something in common. Let's just say that we were introducing colors into our UI.
01:29
Well, we might have a green color for public, and we might have a red color for private. We might also have a red color for failure. We might have a green color for success,
01:40
and we might have, say, an orange color for waiting, or a yellow color for waiting. So let's implement this function within here that we saw earlier. So let's say color. That's going to return to us a string.
01:53
That might be a string for a CSS color. It doesn't really matter. And let's go ahead and use our match statement to give us this value back. So we're going to go ahead and match on this.
02:05
And we're going to say self public. Well, that's going to give us back green. And if we were to have private, well, that's going to give us back a red color. And we can pretty much go ahead and copy this down to here.
02:23
And do the same here. So we've got failure, which is going to be red. And these are in a little bit of an odd order, but it doesn't really matter too much. We have a success, which is going to be green in lowercase.
02:36
And we have a waiting, which is going to be maybe orange, like so. OK, so we've got two enums that both give us a color based on their status. Let's just try out dumping out some of these colors. And then we'll go ahead and look at where an interface is going to help it.
02:56
So we'll go ahead and use the visibility status first of all. And let's say public. And we know that from that, we can just directly access the color. Let's go ahead and run this here.
03:05
And yeah, we've got private and public. So let's just go and change these to one and two. Of course, that makes sense. And there we go.
03:16
We get green for public. Let's go ahead and choose private. And we get red. So these statuses were being stored in the database or somewhere else.
03:26
You would go ahead and deserialize them. And then you can use this color to represent that within the UI. Really, really handy. But both of these do the same thing.
03:37
So it makes sense to have a common interface between these two things. So let's go ahead and create out an interface at the top here called color. And what that's going to allow us to do is to find that these two things, they're both sort of statuses that can represent colors, have a color method.
03:54
So let's go ahead and say color in here. Let's make sure we define that. It needs to return a string. And that's pretty much it.
04:00
We can now go ahead and implement this interface on both of these. And we know instantly now that it has to use this color method. So what we do is we do this after we have declared the return type or the case type. So we implement hasColor.
04:16
And we can do that on the encoding status as well. So let's just go ahead and run this. And we can see here we've got interface hasColor not found. So let's just change that to hasColor.
04:28
And there we go. We get exactly the same thing. Let's just try this with our encoding status as well. And let's say waiting.
04:37
And there we go. Great, we get orange. So basically, this interface means that if we don't provide this method in here and we try and run this, you can see that the interface that we have assigned to this enum
04:48
contains one abstract method. And it must therefore be declared abstract or implement the remaining methods. Basically, put this color method in there because this needs to return a color. And really importantly, that color needs to be a string.
05:03
So it just enforces the fact that we have to have this in here with the return type that we expect. So there we go. Any status, whether it's in the encoding status, the visibility status, or anything else,
05:16
must return a color as a string in here. And that really helps enforce some integrity when we're working with enums that do two similar things. In this case, return a color.
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!