WEBVTT

00:00.260 --> 00:05.630
So what you are seeing right now is a syntax for the arrow functions.

00:05.630 --> 00:09.290
And they're also known as short closures.

00:09.800 --> 00:11.360
So this might ring a bell.

00:11.360 --> 00:17.750
We've already talked about closures in PHP and this was anonymous functions.

00:17.750 --> 00:24.830
Functions without a name that are passed as an argument to a function or associated with a variable.

00:24.860 --> 00:31.640
So those arrow functions were introduced in PHP 7.4.

00:31.670 --> 00:39.890
That's just a more concise way if you need to write short single expression functions.

00:39.890 --> 00:42.590
So this is really important.

00:42.620 --> 00:47.540
Arrow functions can only have one single expression.

00:47.540 --> 00:50.960
And there is an implicit return.

00:50.960 --> 00:59.090
What this means is in normal functions or the other functions than the arrow functions, whether that's

00:59.090 --> 01:00.500
a closure or not.

01:00.650 --> 01:06.070
To return something you need to use the return keyword here.

01:06.550 --> 01:07.840
Well, you can do that.

01:07.840 --> 01:14.590
But also whatever this expression evaluates to is returned from such function.

01:14.590 --> 01:21.730
And well, the other things this function, this arrow function is not using the function keyword,

01:21.730 --> 01:23.680
instead it is fn.

01:23.830 --> 01:30.730
Then you have some optional arguments and instead of the curly braces you have this arrow symbol.

01:30.730 --> 01:34.960
So those two characters and a semicolon.

01:34.960 --> 01:42.460
After this single expression which is implicitly returned, let's now jump to the code editor and see

01:42.490 --> 01:44.230
some actual examples.

01:45.250 --> 01:58.000
So let's make an arrow PHP file, and the plan is to see an array of numbers being squared using an

01:58.000 --> 01:59.710
array map function.

01:59.710 --> 02:02.320
And we're going to have two examples.

02:02.320 --> 02:05.170
So traditional anonymous function.

02:05.170 --> 02:10.770
So you can see how much shorter the new arrow function syntax is.

02:12.510 --> 02:14.760
So this is something super simple.

02:14.790 --> 02:16.080
An array of numbers.

02:16.080 --> 02:17.730
We've used that before.

02:18.120 --> 02:20.460
Then a squared variable.

02:20.460 --> 02:27.900
So we're going to use array map which is essentially like doing a foreach and creating a new array.

02:27.900 --> 02:36.420
But instead of using the for each loop we can call this function the array map that accepts an anonymous

02:36.420 --> 02:45.510
function like that from which we just return n times n.

02:48.270 --> 02:53.220
And we also tell it that the input is this numbers array.

02:53.220 --> 02:57.750
The result stored in squared would be a new array.

02:57.750 --> 03:06.720
Let's just dump this array and also the original one so we can see the results.

03:07.290 --> 03:09.810
So we run PHP or PHP.

03:10.110 --> 03:11.760
There are the two arrays.

03:11.910 --> 03:13.650
That's the original one.

03:13.650 --> 03:16.460
And this is the squared one.

03:17.750 --> 03:21.410
So I think that we have probably did this example before.

03:21.410 --> 03:23.990
So there is nothing special to explain here.

03:24.020 --> 03:30.350
Now let me show you the same example, but this time by using arrow functions.

03:30.380 --> 03:34.100
So the first change is function is change to fn.

03:34.130 --> 03:36.590
Then we just get this parameter.

03:37.760 --> 03:39.770
There is no curly braces.

03:39.770 --> 03:48.320
Instead there is this arrow symbol equality sign and more than placed together there is no return keyword

03:48.320 --> 03:52.970
as arrow functions implicitly return the value.

03:53.120 --> 03:55.400
Let's get rid of this curly brace.

03:55.400 --> 03:59.570
And also in this case there is no need for a semicolon.

03:59.600 --> 04:05.870
I can also move this to some separate lines so it's clearly visible.

04:06.350 --> 04:10.760
This is how this new arrow function looks like.

04:10.790 --> 04:20.350
Instead of this much shorter syntax, and very useful for cases like passing a closure to one of the

04:20.350 --> 04:28.480
built in PHP functions for processing arrays and other different functions that you might not know yet.

04:30.370 --> 04:37.300
Finally, let's assign this to squared two and just make sure that it works.

04:39.010 --> 04:44.800
So there is the output same in both race, which means that it works normally.

04:46.330 --> 04:49.930
Now that's not everything about the arrow functions.

04:49.960 --> 04:54.070
Let me introduce a variable that would be global.

04:54.100 --> 04:57.610
Let me call that multiplier.

04:57.670 --> 05:04.660
So instead of squaring the values from this array we're gonna multiply it by the multiplier.

05:04.660 --> 05:08.800
So let's first do it with this traditional closure.

05:10.000 --> 05:11.350
I can do it like this.

05:11.350 --> 05:21.610
Now you probably remember this won't work as this closure can't access a variable from the outside scope

05:21.610 --> 05:26.640
and it just returns zeros everywhere and there should be a warning on the top.

05:27.000 --> 05:35.730
The variable multiplier is undefined, so traditional closures can't access global variables variables

05:35.730 --> 05:37.860
from outside its scope.

05:38.160 --> 05:46.290
To do it, you need to use this use statement and only then use multiplier.

05:46.290 --> 05:49.560
And then you can use this variable.

05:49.560 --> 05:52.950
So you can see that this is getting really complex.

05:53.040 --> 06:01.770
Now that's not the case with arrow functions as arrow functions as the arrow functions just inherit

06:01.770 --> 06:04.980
the variables from the parent scope.

06:04.980 --> 06:09.570
So arrow functions work out of the box with outside variables.

06:09.570 --> 06:12.540
No need to add any use statements.

06:13.020 --> 06:16.410
If I run this script right now, we get the output.

06:16.410 --> 06:23.370
So everything is properly calculated and there are no issues whatsoever.

06:23.400 --> 06:28.680
As I've said, you can just use outside variables with arrow functions.
