This episode is for members only

Sign up to access "PHP Enums" right now.

Get started
Already a member? Sign in to continue
Playing
07. Getting all enum cases

Transcript

00:00
If we go back to our book example, let's say that we had a form where we could select which book or which option we wanted to store for each book. We're not going to pull this together into a full example because we're just working with a raw PHP document here, but this would be the status within your form and then what you'd want to do is iterate over depending on the
00:24
language that you use and the framework that you use, each of the statuses here as status and you'd want to output the status inside of this option. Of course you most likely don't want to hard code this into your form, so this would eventually end up as one, two, three or however many statuses you had and of course the display value could be the present tense method that we've looked at.
00:48
You could have a separate method to output what you wanted to see in a form, but basically we want to be able to build up a form of all of the potential statuses or whatever within our enum that we have. So we're not going to actually pull this together as a full example, so let's close this off and let's look at how we can iterate through each of the values within our status.
01:09
Now this is very easy to do. Let's go ahead and just var dump on our status here or our book status. Let's just bring that back so it's a little bit clearer and all we do is we call the cases method. So again going back to the first episode where we looked at having a class, just a plain php class to deal with this, we wouldn't have this functionality baked into that. We would have to do
01:30
it ourselves, so that's another benefit of enums. So let's go ahead and just run this and you can see sure enough we get back an array with each of the enum objects. Now that means that what we could do is implement a method in here like we did before. So let's say that we just had a text method. I'm just going to keep this really really simple. I'm sure you can think of a better name
01:51
and we used our match in here to match this up. So let's go ahead and say match on this and we want to go ahead and say self want to read and we want to return want to read. So that would just be like a standard text representation of this enum case. Let's just get rid of red and just do two of these just to keep it shorter and let's say reading and reading. So now that we've got this text
02:23
function in here or method in here, that's not going to do anything to the output because we know that we just get the enum object itself back. But what we can now do technically is iterate over these in our template like we just saw an example of. So we could say book status cases as status and let's just var dump these on each line. So status and of course that's going to
02:48
give the object back but what we can now do is invoke that method within our template to grab back the actual value that we want to show to the user. So that's going to give us back each of these strings on each line and of course these would go inside of that option tag that we saw earlier. And of course what we can also do is output the value as well so that value could go
03:09
within the value within your option. So basically you've now got a much more powerful way of building up this drop down with all of the potential statuses and really importantly these are now all in one place and we don't have to worry about updating this from multiple locations. This is just going to tend to work everywhere now. If we update this enum with another status
03:32
that's going to be output in the form. We're going to be able to store that in the database. We're going to be able to deserialize that so we can go ahead and grab any value from that. So just using a really simple method in here which is baked into enums we can iterate through these and output these statuses for the user to choose within a form.

Episode summary

In this episode, we dive into how to get all the cases from a PHP enum, which comes in super handy when you need to build forms or dropdowns dynamically. Using our book status example, imagine you want a select input so users can pick the status of a book. Instead of hardcoding all your statuses in the form, we show how enums let you generate this list automatically.

We explore the cases method, which returns all your enum objects as an array. You can then loop over these and display them however you want—like as <option> tags in a form. The beauty here is when you add a new status to your enum, it automatically appears in your form everywhere you use this method, so your code stays DRY.

We also add a simple text() method to our enum that returns a user-friendly string for each status. This makes your forms look much better, since you can show readable names instead of enum constants. It's a flexible setup that means you don't have duplicate code all over the place if things change later.

So, by just using a built-in enum feature, you get an easy way to build up dynamic, maintainable dropdowns or lists based on the current enum values.

Episode discussion

No comments, yet. Be the first!