---
title: 'PHP Enums Explained: How to Use Enums in PHP 8.1+'
source: 'https://youtube.com/watch?v=pS9FbYKbHVs'
video_id: 'pS9FbYKbHVs'
date: 2026-07-31
duration_sec: 857
---

# PHP Enums Explained: How to Use Enums in PHP 8.1+

> Source: [PHP Enums Explained: How to Use Enums in PHP 8.1+](https://youtube.com/watch?v=pS9FbYKbHVs)

## Summary

This video provides a beginner-friendly introduction to PHP enums, a feature added in PHP 8.1. It walks through the limitations of using constants, demonstrates how to define and use basic and backed enums, and explains the from() and tryFrom() methods with practical examples.

### Key Points

- **Enums in PHP vs other languages** [00:13] — Enums have existed in languages like C# for a long time and have now been added to PHP.
- **Old approach using constants** [00:27] — Before enums, developers used constants like STATUS_PENDING, STATUS_ACTIVE, and STATUS_INACTIVE to represent fixed values.
- **Problem with constants** [01:39] — Constants don't enforce type safety; any string such as 'banana' can be passed, causing silent bugs.
- **Defining an enum** [02:08] — Enum syntax is similar to C#: define an enum with a name and cases, e.g., enum Status { case Pending; case Active; case Inactive; }.
- **Accessing enum cases** [03:17] — Use double colons to access a specific case, e.g., Status::Active. The built-in 'name' property returns the case name as a string.
- **Strict comparison with enums** [03:58] — Use the identical operator (===) to compare enum cases, ensuring both type and value match.
- **Backed enums** [04:25] — You can attach scalar values (string or int) to each case, e.g., enum Status: string { case Active = 'active'; }.
- **name vs value properties** [04:55] — The 'name' property outputs the case name (i.e., 'active'), while the 'value' property outputs the attached backed value (i.e., 'active' as well).
- **Converting values with from()** [06:23] — The from() method converts a scalar value back into an enum case. If the value doesn't match any case, it throws a ValueError.
- **tryFrom() returns null** [08:00] — tryFrom() does the same as from() but returns null instead of throwing an error when the value is invalid.
- **Best practice: when to use tryFrom() vs from()** [09:11] — Use tryFrom() for user input or external data you don't control; use from() for internal data (like your own database) where an invalid value indicates a bug.
- **Enums as type hints** [10:30] — Using an enum as a parameter type (e.g., Status $status) ensures only valid enum cases can be passed, preventing invalid strings like 'banana'.
- **Enums vs constants** [13:14] — Enums bundle all valid values in one place and enforce type safety, making them a more robust replacement for constants.

### Conclusion

Enums are a powerful way to enforce valid values in your PHP code. Using them instead of constants for fixed sets of statuses or priorities makes your code more robust and self-documenting.

## Transcript

from PHP for the longest time has been something called enums, which is something we've had in other languages such as C# at least I've used it in C# when I made games inside Unity. So, I think the best way to sort of explain
enums is to give an example of how we would actually do code in the past versus how we can now do it using enums. So, let's say I have a website with a login system and there's a bunch of users inside this website and I want to
have a certain status. We want to see if the request to sign up to the website is still pending. We want to see if the user is actually active or if the user is inactive. Then in the past, we would
create a bunch of constants like I have here, a constant called status pending, would have a value that we could then use to compare different things, right? So, inside a function, we would then
create a piece of code that basically just goes in and says, "Well, okay, we want to set user status the old way." I wrote in here and basically just bring in a status. So, if I go below here, I'm just going to copy what I have here.
Let's say I go below here and as I want to run this function, so set user status the old way and then I just pass in status active. You know, I'll just pass that in here. So, if I do this, then this would actually go ahead and set the
this would actually go ahead and set the user to active because status active is equal to active. Now, the problem here, like I wrote in this example down here, is if I were to run this function and just paste in banana,
that would also work. There's no thing preventing us from doing this because I just need to pass in a string. I could also pass in my name. I could pass in any sort of value and this would actually work. It wouldn't give me a
error message because all I'm asking for is a string. So, what we can then do using enums is we can actually predetermine values inside an enum and say that the value has to be one of these values. And
it has to be a enum that we pass into our function. So, if I go to the next page here, you can see that now instead I created a enum. And this is very familiar if you know a little bit about C# cuz this
looks pretty much identical to how C# does it. Uh but basically we define a enum and we call it a certain name. So, in this case I call this one status. And I just basically create some cases. So, in this
case here I created a pending, I created a active, and a inactive. Just like we active, inactive. But instead, these three are uh cases
inside this enum here. So, what I can then do is I can actually use these inside as a parameter for setting a status status inside, you know, a piece of code that
can't just write banana. That would actually throw me an error message because that is not, first of all, is not a val- valid value, but second of enum. And a couple of different ways we can interact with this enum here is by,
for example, accessing one of the cases inside the enum. So, I can say variable inside the enum. So, I can say variable equal to the name of the enum to colons this case here this would be active. I'm trying to access the the active case
inside the enum. Now, we can also refer to something called a built-in property, which in this case, if we point to name, uh is also going to point out or output
active. Uh we can also use comparisons. So, if I were to say I have a if and I want to check if this user status I created up here, which is right now I created up here, which is right now equal to the active enum case, I can
check if it is equal by writing three equal signs, not two, but three, cuz we possible. And I want to check if it is equal to And I want to check if it is equal to the enum called active. If they are
equal, then I want to echo out user is active. So, in this new example here, I do also have a enum example, but in this case here, we are defining this one as a string. So, I'm saying that we have a enum called status backed.
And in this case here, this is a string value that each of these enum cases are going to have. So, you can actually attach a value to each of these cases. So, I still have pending, active, inactive, just exactly like we did
before. But now I do also have this value here attached to them that I can use for certain things. So, if I go below here, uh you can see that we can also access the value property. So, in the previous
example, we accessed the name. So, if I were to say that this is actually have an example down here, active points to the name property, then this would spit out active, because this is the name property. And if I say I want to spit
out the value property, I'm going to spit out the actual you know, whatever this is equal to. So, in this case here, the active string that we have up here. So, in this case, we can also access these two different
properties that we have inside this enum example here. And of course, we can also use other different types of data types. So, in this case here, I have a integer data type, so an enum called priority, which is a integer data type. And this
one also has cases, so low, medium, high, and a value called 1 2 3. And then again, if I want to echo out the values of these different cases here, I can just access the enum, then point to a case with the two colons
there, and then point to a value property. And we do also have a couple of different methods we can use on these enums here because let's say I have a value, let's say from a database. I am trying to figure out if this user is
trying to figure out if this user is currently active by pulling out a col- column from a database of this particular user here that is called is user active or something. And I want to compare that to my enum inside my code.
How do we do it the other way around? Well, we can use something called from inside PHP, which basically just converts the values back into enums. So, an example here, if I were to use from,
if I have a variable called status that is equal to status backed, which is again this previous example we had here, I can actually just copy paste it so we can actually see it. If I paste it on top of here,
if I want to take and check a certain value up against one like this particular enum here, one of the cases inside, I can say that I want to set a variable equal to status backed colon colon and then use the from method and
colon and then use the from method and check if active is a valid value inside this particular enum. So, in this case here, active is an actual value inside the active case. So, in this case here, if I were to echo
So, in this case here, if I were to echo status and point to the name property, it would then spit out active because status is equal to this particular enum that has that particular string inside of it. In this case here,
the name, we could also do value and this would then spit out, you know, active. And the thing here is that when we use the from method, this would we use the from method, this would actually create a value error if, let's
say, I go in and write banana because banana does not actually exist inside this particular enum up here. So, this is going to throw a value error inside if this does not exist. And you can actually see my IntelliSense here or my
thing. So, that's kind of cool, which is why we have another example down here, which is try from, that basically does the exact same thing as from, but instead of throwing a error that is actually going to cause an error message
inside the website if that particular value does not exist inside the enum that we passed in and tried to check for, then it's going to just return null. So, in this example down here, I am trying to do the exact same thing as
in banana inside the try from, and because banana does not exist inside this enum up here as one of the values on the right side of here, it is going to just return null. And then we can throw a custom
message inside the website. So, let's say it does return null, then I can just create an if statement says, "Well, if status right now is equal to null because banana does not exist, then I can just echo out some sort of
message on the screen, you know, so the people inside the website know they did you know. We can do many different things here. So, it is sort of best practice when using each of these two different
using each of these two different methods that whenever we want to get some sort of data from a user inside your website or external data that we don't have any control over ourselves as a as a developer,
a as a developer, then we want to use try from
method, which is going to cause a error message inside the website, then because being, you know, grabbed and checked for, we might end up getting error messages constantly inside the website
instead of having some sort of code that handles if something is wrong. So, you know, if you're grabbing something that you know the value has to be a certain way, for example, your own database, then use from
wrong, then you know that it is something wrong with your code. So, you have to use from in order to actually output an error message. But if it's something from a user, an external website, then use try from so you can
actually control how that sort of error should be handled inside your website. So, to give an example here, let's take the same enum example we've been using for the past couple of examples here. Um let's say I want to create a function
basically we just want to update the user status based on this enum here. What I can then do is paste in two parameters, one called maybe the user ID to fetch the correct user from inside
the database. And then I can also pass in a enum. So, status backed as a data here. I can also just unwrap it so it's a little bit easier to see. Uh we're passing in status backed as the
actual value or the the data type for this value, and then we can just create a placeholder called variable status. And this is going to return void, which return a value. This is just going to do something inside this function here. So,
inside the actual function, we just want to echo out updating user and then the to echo out updating user and then the user ID to this particular value here. So, we're basically just ensuring that whatever we want to update the user to
does not turn out to be banana or something because that could potentially be an error that happens inside your code. Uh so, by passing in an enum instead, we're absolutely sure that if something goes wrong, then we do
actually get an error message because we want to pass in an enum value uh some sort of random string or something else that can cause, you know, weirdness inside the website. Uh so, the way I would use the function down here
below is to simply refer to the function, pass in one as my user ID. So, this is the first user inside my website. And then, simply pass in an enum called status.backs {colon}{colon} active, which now means that I'm setting
active, which now means that I'm setting this uh user here as active inside my database if I were to, you know, update my database using this particular uh function here. An example of doing this the wrong way would be to use this
example I have below here where I take the same function called update user status, and I again pass in a uh integer as a user ID, which is correct. But, if I were to try and pass in a string called, for example, active
or banana or whatever, this would actually give me a type error because this is not an enum. This is me just trying to pass in a string, and this is where things are a little bit different than the, you know, the old way we did
things by using constants because if I did not require this to be a enum type, then I could just pass in banana, and it would actually try and go in and and it would actually try and go in and update my database by setting my uh user
status to banana. So, this is why we want to, you know, use enums whenever we want to do something very specific when it comes to checking for statuses or,
bunch of constants that, you know, these are the values that we want to use. Instead, we can just create an enum that contains all the values and ensures that we can only use those enums whenever we want to pass in data
into, let's say, a function. With that said, this was a short introduction to enums and there's a lot more to cover when it comes to enums. I do have a whole bunch of notes on the side here that we could also cover with examples
and such things. Um but this is just kind of like an introduction to enums and something that I think that people should look into if you uh are still enum is yet because it is something we use in C#. And it's just kind of a neat
use in C#. And it's just kind of a neat thing to use inside C#. So, with that said, I hope you enjoyed and I'll see you guys next time.
