WEBVTT

00:00.410 --> 00:07.100
Okay, so next up, I'd like us to learn about the types of the function arguments and the optional

00:07.100 --> 00:09.680
return types that you can define.

00:09.920 --> 00:13.970
Now let's go back to the data types as well.

00:14.090 --> 00:15.620
Let me zoom in.

00:16.490 --> 00:22.760
So we're gonna create this example in a separate file now because this time we're gonna be talking about

00:22.760 --> 00:24.140
the explicit types.

00:24.140 --> 00:28.640
And how can we enforce the type to be explicit.

00:28.640 --> 00:35.720
Which means that you shouldn't really be able to pass strings when the function expects integers.

00:35.750 --> 00:40.460
So let's jump to the code editor and see what does it all mean.

00:41.630 --> 00:52.370
So let's make a types file inside the functions folder, add a PHP tag, and let's define a super simple

00:52.370 --> 00:53.060
function.

00:53.060 --> 00:59.420
But this time we're gonna have types for the arguments and for the return type of that function.

00:59.420 --> 01:02.580
So this function is super simple.

01:02.580 --> 01:03.990
It's just adding.

01:03.990 --> 01:11.370
Now obviously we have an operator for the adding operation in PHP, but that's just an example.

01:11.370 --> 01:14.070
So let's not think about it too much.

01:14.070 --> 01:21.180
And when we add the values we have basically one value that we add to the other.

01:21.180 --> 01:26.910
So let's define two arguments of the type integer a and b.

01:27.030 --> 01:31.080
And obviously the return type of that has to be integer.

01:31.080 --> 01:32.790
So let's also define that.

01:32.790 --> 01:33.840
It's super simple.

01:33.840 --> 01:37.590
We're just going to return a plus b.

01:38.100 --> 01:46.020
So as I said let's ignore the fact that creating such function has zero sense because this operation

01:46.020 --> 01:54.630
exists I just want to present something okay, so why don't we just echo the result of add and let's

01:54.660 --> 01:57.000
add 5 to 3.

01:59.040 --> 02:00.060
Let's run it.

02:00.100 --> 02:04.270
Um, let's make sure I am in the functions folder and run types.

02:04.510 --> 02:05.020
PHP.

02:05.620 --> 02:07.930
So that's the expected result.

02:07.960 --> 02:08.890
Eight.

02:09.100 --> 02:13.210
But what happens if I would pass a string here?

02:13.210 --> 02:17.410
That would be a number but passed as the string type.

02:17.410 --> 02:19.210
So let's run this again.

02:19.510 --> 02:22.690
PHP types PHP and we still got the result back.

02:22.720 --> 02:23.320
Eight.

02:23.350 --> 02:26.950
Now why won't we vardump that instead.

02:26.950 --> 02:29.500
So we will also see the return type.

02:30.010 --> 02:35.110
Let's do var dump and rerun that again.

02:35.110 --> 02:40.210
So the result of the function is an integer value is eight.

02:40.210 --> 02:41.680
So what happened here.

02:41.680 --> 02:45.580
We've defined the types and we were explicit about that.

02:45.580 --> 02:52.000
So what's the point of defining the types if you can pass a string to a function.

02:52.000 --> 02:55.390
So let me explain what's happening here.

02:56.410 --> 03:05.060
So generally the type declarations were introduced in PHP around the version five, and they were just

03:05.090 --> 03:06.380
added gradually.

03:06.800 --> 03:10.040
PHP was moving really slow in the past.

03:10.070 --> 03:15.650
Now a lot more features are added and a lot more quicker and a lot more often.

03:15.650 --> 03:18.350
But that wasn't the case back then.

03:18.350 --> 03:23.360
So you've got the types, but they weren't strictly enforced.

03:23.660 --> 03:34.940
Now, since PHP version seven, another option was added that was strict types, which basically meant

03:34.940 --> 03:43.520
that you had to tell PHP that if you define the types you really mean that that you are not just signaling

03:43.550 --> 03:51.590
what would be nice to pass to a function, but that a value of this specific type has to be passed to

03:51.620 --> 03:52.340
a function.

03:52.340 --> 03:58.400
Otherwise there will be an error, a runtime error, and the program will stop.

03:58.430 --> 04:01.510
So to actually enforce this.

04:01.540 --> 04:08.560
To enforce those types, you need to add a declaration at the top of the file which is looking exactly

04:08.560 --> 04:09.280
like this.

04:09.280 --> 04:17.320
You write declare and you type strict underscore types equals one.

04:17.350 --> 04:25.570
By the way, that's one of the well, one of the couple statements, one of the exceptions in PHP that

04:25.570 --> 04:28.270
does not require semicolon.

04:28.360 --> 04:33.490
So I've mentioned that before that you don't have to add semicolon everywhere.

04:33.520 --> 04:37.570
That's one of the cases where you don't have to do that.

04:37.600 --> 04:40.390
Another is for example namespace declarations.

04:40.390 --> 04:44.860
But we're gonna get to that when we'll be talking about classes.

04:44.860 --> 04:49.000
But I prefer to just add those semicolons everywhere.

04:49.030 --> 04:51.280
My personal preference.

04:51.700 --> 04:54.160
Now let's see what will happen now.

04:54.790 --> 04:58.300
Now we have strict types, but only for this file.

04:58.300 --> 05:04.220
So you need to understand that that you have to add this declaration in every single file.

05:05.210 --> 05:11.870
Now, when I run the same code and I'm passing a string to this function, I get a fatal error.

05:11.900 --> 05:18.440
And I would really suggest to declare the strict types to be enabled, because that's the whole point

05:18.440 --> 05:27.410
of having types, the added security, making sure that no one will accidentally pass something that's

05:27.410 --> 05:35.030
of a different type, which forces PHP to use type coercion, which might have some unexpected side

05:35.030 --> 05:35.870
effects.

05:35.870 --> 05:44.090
So this type coercion, that was fine when PHP was used to create apps that maybe weren't so mission

05:44.090 --> 05:45.260
critical.

05:45.290 --> 05:53.480
But these days when we are creating software that, for example, handles money well, you probably

05:53.480 --> 06:04.140
want to have all the security that you can get So to make it work, I need to change it back to five.

06:04.980 --> 06:08.610
And now we get the working function.

06:09.540 --> 06:11.280
So why we are here?

06:11.280 --> 06:15.180
Let's do an experiment and let's pass a float value.

06:15.180 --> 06:22.710
But we are gonna pass something that is, at least for us, exactly the same.

06:22.740 --> 06:25.020
So I'm going to pass 5.0.

06:25.020 --> 06:28.260
And let's see what PHP is gonna say about that.

06:29.760 --> 06:30.090
Okay.

06:30.090 --> 06:32.190
So that's a fatal error.

06:32.250 --> 06:39.720
So as you see if that's an integer, it has to be an integer, not something that looks like an integer

06:39.720 --> 06:42.300
or is almost an integer.

06:42.330 --> 06:44.520
It has to be an integer.

06:44.520 --> 06:47.370
And it really has to be of this type.

06:47.370 --> 06:54.870
So if you really want to pass this as an integer, you can typecast it using this int operator.

06:54.900 --> 06:57.090
Then it's fine.
