Next up, we can use an enum keyword to define an enum or enumeration.
Then you can add cases to add specific possible values of this enum, like days of the week.
And what this does is we create a type safe way to represent some set of values.
Now let's see how can you use an enum to assign it to a variable?
We're going to use the name of the enum double colon operator and just choose one of the options like
Wednesday.
So enums are also classes.
So we always use the enum values using the enum or class name.
The double colon operator and one of the options and today variable is now of the type.
Days of the week.
That's not a string and not an integer.
If you would like to then compare the value to one of the enum values you use strict equality operator.
So three equal signs and you need to compare it to one of the possible values.
Like Wednesday here we can confirm it is Wednesday.
So that's a type safe comparison.
And PHP will make sure we are actually comparing the same type.
Let's define another enum.
This time it would be a color and enums can have a so-called backing.
So backing is defining this optional values.
Now let me show you how do you read those values which can be strings or integers.
So if specific variable has the value of one of those constants from the enum, you can also get the
actual value from it.
So if the color is red to read the value, you're going to use the arrow operator.
And this should display this backing value.
I think I've used the wrong class name here.
Let's fix that quickly.
And now let's run that.
So there it is.
The comparison works fine.
And also we've got the value of an enum this way.
Now you can use enums for type hinting.
Let's say we have an is weakened function.
We can type in the argument using days of week.
This would return a boolean, and the logic would be whether the day is exactly days of week Saturday
or the day is exactly days of week Sunday.
Let's test this.
So we run this weekend.
Maybe I'm gonna run this for Monday, and I'm going to use the ternary operator to display either yes
or no.
And finally let's run it.
So the answer is no here okay.
So to summarize enums they are not really complicated.
They let you create a new type with some fixed set of possible values.
And they provide type safety.
And this basically prevents you from invalid values being used.
The values you didn't expect to be used.
Your code might be cleaner, more self-documenting you can have the backup values that might have some
more meaning.
You can use enums in type hints, and generally they will probably let you prevent some errors and make
your code more readable and easier to understand.