WEBVTT

00:00.170 --> 00:07.370
Next up, we can use an enum keyword to define an enum or enumeration.

00:07.940 --> 00:18.170
Then you can add cases to add specific possible values of this enum, like days of the week.

00:18.200 --> 00:26.390
And what this does is we create a type safe way to represent some set of values.

00:26.420 --> 00:31.010
Now let's see how can you use an enum to assign it to a variable?

00:31.070 --> 00:38.510
We're going to use the name of the enum double colon operator and just choose one of the options like

00:38.510 --> 00:39.470
Wednesday.

00:39.500 --> 00:41.660
So enums are also classes.

00:41.660 --> 00:46.790
So we always use the enum values using the enum or class name.

00:46.970 --> 00:54.560
The double colon operator and one of the options and today variable is now of the type.

00:54.590 --> 00:56.180
Days of the week.

00:56.180 --> 00:59.870
That's not a string and not an integer.

01:01.100 --> 01:08.840
If you would like to then compare the value to one of the enum values you use strict equality operator.

01:08.840 --> 01:15.680
So three equal signs and you need to compare it to one of the possible values.

01:17.930 --> 01:24.830
Like Wednesday here we can confirm it is Wednesday.

01:27.740 --> 01:30.680
So that's a type safe comparison.

01:30.680 --> 01:36.020
And PHP will make sure we are actually comparing the same type.

01:36.050 --> 01:38.150
Let's define another enum.

01:38.150 --> 01:45.230
This time it would be a color and enums can have a so-called backing.

01:45.230 --> 01:49.640
So backing is defining this optional values.

01:49.670 --> 01:54.770
Now let me show you how do you read those values which can be strings or integers.

01:54.770 --> 02:04.040
So if specific variable has the value of one of those constants from the enum, you can also get the

02:04.040 --> 02:05.660
actual value from it.

02:08.920 --> 02:16.120
So if the color is red to read the value, you're going to use the arrow operator.

02:16.120 --> 02:19.840
And this should display this backing value.

02:19.870 --> 02:22.660
I think I've used the wrong class name here.

02:22.660 --> 02:23.920
Let's fix that quickly.

02:23.920 --> 02:27.310
And now let's run that.

02:28.210 --> 02:29.320
So there it is.

02:29.380 --> 02:32.980
The comparison works fine.

02:32.980 --> 02:36.910
And also we've got the value of an enum this way.

02:37.060 --> 02:40.870
Now you can use enums for type hinting.

02:41.710 --> 02:46.810
Let's say we have an is weakened function.

02:46.810 --> 02:51.070
We can type in the argument using days of week.

02:52.780 --> 03:03.430
This would return a boolean, and the logic would be whether the day is exactly days of week Saturday

03:04.330 --> 03:11.470
or the day is exactly days of week Sunday.

03:12.340 --> 03:13.720
Let's test this.

03:13.720 --> 03:15.910
So we run this weekend.

03:16.960 --> 03:25.360
Maybe I'm gonna run this for Monday, and I'm going to use the ternary operator to display either yes

03:25.390 --> 03:27.490
or no.

03:27.490 --> 03:30.610
And finally let's run it.

03:30.610 --> 03:32.680
So the answer is no here okay.

03:32.680 --> 03:36.250
So to summarize enums they are not really complicated.

03:36.250 --> 03:41.200
They let you create a new type with some fixed set of possible values.

03:41.200 --> 03:43.720
And they provide type safety.

03:43.720 --> 03:49.330
And this basically prevents you from invalid values being used.

03:49.330 --> 03:52.750
The values you didn't expect to be used.

03:52.750 --> 04:00.550
Your code might be cleaner, more self-documenting you can have the backup values that might have some

04:00.550 --> 04:02.050
more meaning.

04:02.050 --> 04:11.410
You can use enums in type hints, and generally they will probably let you prevent some errors and make

04:11.410 --> 04:16.240
your code more readable and easier to understand.
