This episode is for members only

Sign up to access "PHP Enums" right now.

Get started
Already a member? Sign in to continue
Playing
05. Enum methods

Transcript

00:00
OK, so we're returning back to our backed enum with the values 1, 2, and 3,
00:04
which we know on their own in any kind of output may not be very helpful. Now what we're going to do is decorate this enum with a function which will return to us something more useful based on the enum type. That sounds a little bit more complicated than it is.
00:20
Let's just go back to the value that we looked at before trying to grab this enum from a value and let's say book status and we'll say try from and let's just pick want to read. So we know that if we go ahead and use var dump or output this enum, we're going to end up with the enum object here.
00:41
Now we want to know how to grab a useful value from this. Of course, we already know that we can grab the value itself, which gives us one, but we want to grab something a little bit more useful. So let's just say that we were outputting in our UI,
00:56
Alex wants to read this book based on the book title or Alex is reading this book or Alex has read this book. That's like a present tense thing. So let's go ahead and introduce a method inside of this enum called present tense
01:13
and that's going to go ahead and return to us a string that we could potentially output on our UI. Now what we can do inside of here is we can use maybe a PHP match statement in here. We can pass in this because technically we're reading the actual enum itself and then we can go ahead and give cases in here.
01:30
So status want to read and we can say well if this enum has this status, we want to return wants to read. That's going to be something really useful that we can use to output in our UI. We can do the same for all of the others.
01:44
So reading and is reading or in this case the actual text is reading and of course finishing off with read. So this would be has read. So now that we've got this method in here,
01:59
that means that we can grab our status or our book status in this case. So let's just switch this over to book status and we can actually use this present tense method to grab back the correct string based on the type of enum we have.
02:13
So let's go from the start. Let's imagine that we have the value one stored in our database and we want to go ahead and grab the present tense from that after we've pulled it out of the database, deserialized it
02:25
and we want to output this to our UI. Well let's just go ahead and do this now and there we go. We get wants to read. So you could do anything in here.
02:33
You could use a switch statement. You could use a series of if statements if you wanted to. It really doesn't matter what you do. Match just tends to be a little bit shorter and works really well within here.
02:44
Just to give you an example of that, if you are really new to enums, let's just go ahead and say if this equals one. So we're basically checking the enum itself
02:56
and this will be implicit and grabbing the value. Then we want to return wants to read and then of course we could have some sort of fallback as well in here and just return an empty string or a null value
03:10
and then we could go ahead and choose that we wanted to also return a null value. So let's just switch this back to make sure it's a one and you can see that we get exactly the same result here if the value, sorry, is one.
03:23
There we go. We get wants to read. So you could introduce a series of if statements if you want to do a switch. Whatever works best for you but I find that match here makes a lot of sense to keep this nice and clean.
03:34
So step one, if we just think about this in terms of a real life application, would be to store the enum value, e.g. one, in the database. When we pull this out we get the value one but we use try from or from to grab the enum object out
03:52
and then from there we can call a method on this to represent this within our UI. So if we were passing the actual model itself down to our UI we just call a method on this value that we get. Now this is also helpful if we bring back our API example.
04:09
So let's just make this really really simple and we'll create a response out here maybe and let's go ahead and add a status in here. We won't bother with the title, we'll just keep this really simple
04:20
and if we wanted to var dump out the JSON encoded response that we want to send for our API response and we have a look at this you can see sure enough the status is now wants to read and you can introduce as many of these methods as you want
04:37
so you could create a method that returned pretty much anything in here you could even return a method that gives you these kind of keys back if you wanted to it doesn't really matter but the point is if we think back to the example we looked at in the first episode
04:50
when we saw using a standard class here for this the functionality was nowhere near as powerful we couldn't just do match this we'd have to do all sorts of things to get this working
05:00
this is a much nicer way to do exactly what we need with a set of constant values now one quick tip before we go we're using book status here to grab the value or the key here that we want to check for we could actually really tidy this up by just referencing self
05:19
because we know that an enum is an object so this makes it a little bit more easy to work with and a little bit more easy to read you can see it works in exactly the same way
05:28
it's entirely up to you you could reference it by itself or just use the self keyword in here to grab back the entire object check the value and return everything that you need
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!