WEBVTT

00:00.290 --> 00:00.680
Okay.

00:00.680 --> 00:06.050
So next up let's learn the anonymous functions also called closures.

00:06.110 --> 00:12.650
The main difference between the anonymous functions and the traditional functions is that the anonymous

00:12.650 --> 00:22.580
functions don't exist under a specific name, which means that you need to assign this anonymous functions

00:22.580 --> 00:30.560
either to a variable, or you need to pass it as a parameter to a function.

00:31.010 --> 00:34.100
It can't really exist on its own.

00:34.100 --> 00:36.500
That's the main conclusion.

00:36.500 --> 00:44.390
And in PHP, like in many other languages, functions are called first class citizens.

00:44.390 --> 00:48.980
What this means is exactly what I've described a second ago.

00:48.980 --> 00:56.150
This means you can assign functions to variables or pass them around as function arguments.

00:56.570 --> 01:00.180
Okay, so that's a little bit of theory.

01:00.210 --> 01:05.250
Now let's jump to the code editor and let's see some examples.

01:05.250 --> 01:14.490
Let's just call this anim PHP and let's see some anonymous functions also called closures.

01:15.690 --> 01:18.510
So let's create an anonymous function.

01:18.510 --> 01:22.860
And it will accept a name parameter.

01:23.370 --> 01:29.550
And it will simply return the hello text with the name.

01:30.990 --> 01:32.820
Now let me add a semicolon here.

01:32.820 --> 01:36.720
And well how do we call this function.

01:36.990 --> 01:37.560
Yes.

01:37.560 --> 01:38.730
So that's the problem.

01:38.760 --> 01:43.050
The anonymous functions or closures as they are also called.

01:43.080 --> 01:46.320
They don't exist under a given name.

01:46.320 --> 01:55.770
That's why you need to associate them with something like associate them with a variable like this greet

01:55.770 --> 01:56.610
variable.

01:56.610 --> 02:09.390
Now this variable is essentially the function name and to call it I can echo, greet and say maybe David.

02:11.700 --> 02:15.390
Now let me call this, uh, this is anon PHP.

02:15.570 --> 02:18.120
As you see, this has worked fine.

02:20.610 --> 02:20.910
Okay.

02:20.910 --> 02:23.730
So let's see yet another example.

02:24.210 --> 02:29.160
This time we won't be associating the anonymous function to a variable.

02:29.160 --> 02:33.180
Instead we're going to pass it to another function.

02:33.210 --> 02:37.020
First things first, let's create some example data.

02:37.050 --> 02:39.330
This would be this numbers array.

02:39.360 --> 02:43.830
We're gonna calculate the square of every element in this array.

02:43.830 --> 02:48.570
And the result will be put into a new array.

02:49.530 --> 02:51.510
Let's call it squared.

02:51.540 --> 02:56.580
Now we are gonna use an array map function.

02:56.610 --> 03:06.070
This is a built in PHP function that will call our anonymous function on every item in the array.

03:07.180 --> 03:14.230
So here we create an anonymous function in place and it's going to receive a number.

03:15.130 --> 03:19.960
And we're just going to return the number times number.

03:19.990 --> 03:22.300
This is how you calculate the square.

03:22.300 --> 03:27.070
And the second argument to array.map is the source array.

03:27.190 --> 03:29.860
This is our numbers.

03:32.290 --> 03:35.770
So this time I'm gonna call Vardump.

03:35.770 --> 03:38.350
So we also see the type.

03:38.470 --> 03:45.040
And let's output squared and maybe the numbers the original array.

03:45.070 --> 03:45.670
Okay.

03:45.700 --> 03:47.110
Let me run it.

03:47.350 --> 03:51.010
That's the original array 123 the numbers.

03:51.010 --> 03:55.570
And here we've got the squares of every single number.

03:55.570 --> 04:03.380
So this is another use case of anonymous functions, where you pass them to functions that work with

04:03.410 --> 04:09.920
arrays or maybe strings, so that you can perform some operation in place.

04:09.920 --> 04:18.800
And it wouldn't make a lot of sense to create a separate named function if what you are going to do

04:18.830 --> 04:23.750
will be applied only in one specific instance, like in here.

04:26.390 --> 04:26.720
Okay.

04:26.720 --> 04:28.190
So now another thing.

04:28.190 --> 04:36.560
So accessing variables in your anonymous functions that are outside the anonymous function itself.

04:36.800 --> 04:40.520
For an example let's copy this fragment.

04:41.060 --> 04:46.790
And here I don't want to actually say hello all the time.

04:46.790 --> 04:54.110
I'd like to use a message that is not passed as an argument to this anonymous function.

04:54.110 --> 04:55.340
Instead.

04:55.340 --> 05:00.470
That's just a variable defined outside this anonymous function.

05:00.470 --> 05:01.520
So it can be.

05:01.550 --> 05:02.570
Hello.

05:02.600 --> 05:05.510
Maybe I'd like to change it to by.

05:08.270 --> 05:08.900
Like this.

05:08.900 --> 05:11.210
Maybe let's also call this variable.

05:11.210 --> 05:12.260
Otherwise.

05:12.260 --> 05:19.670
Um maybe greet two didn't have any better idea.

05:19.670 --> 05:21.590
Anyway, let's run that.

05:21.590 --> 05:31.640
So PHP and PHP and we've got a warning that we are accessing an undefined variable message.

05:31.670 --> 05:34.880
Yet here we can see it is defined.

05:34.970 --> 05:42.950
So the thing with anonymous functions is that they don't have access to the variables defined outside.

05:42.980 --> 05:45.980
That's just a thing to access them.

05:45.980 --> 05:51.620
Well it's possible you can access the outside variables in your anonymous functions.

05:51.620 --> 05:59.790
When you also add this use statement after a function, and here you list the variables you'd like to

05:59.790 --> 06:03.300
be made available to your anonymous function.

06:03.660 --> 06:12.210
So after I define this, that I want this message variable to also exist in the context of this anonymous

06:12.210 --> 06:13.560
function.

06:13.680 --> 06:16.140
This should now work.

06:16.140 --> 06:19.530
And it does right.

06:20.790 --> 06:23.640
Now let's also think about this variable.

06:23.640 --> 06:26.550
So as the name suggests variable.

06:26.550 --> 06:28.890
It's something that can change.

06:28.890 --> 06:32.580
So you can change the value that variable holds.

06:32.580 --> 06:40.380
So can we change this variable the message that we get access to inside this function.

06:41.100 --> 06:42.480
Let's try that.

06:42.480 --> 06:46.290
Maybe I can use the original value.

06:46.320 --> 06:51.810
Concatenate that with an exclamation mark first and only then use it.

06:51.810 --> 06:56.230
So let me run this anon fbx file.

06:56.320 --> 07:06.340
As we see we had the exclamation mark, but after we have called this function, let's echo the message

07:06.370 --> 07:10.030
and let's see if its value has changed.

07:13.000 --> 07:15.340
Okay, let's run it again.

07:16.960 --> 07:25.210
So what happened here is we've modified the message, and this was reflected in whatever was returned

07:25.210 --> 07:27.220
from this anonymous function.

07:27.220 --> 07:34.540
But the original message variable defined here didn't changed and contains the original value, the

07:34.540 --> 07:37.150
original text that was set here.

07:37.150 --> 07:46.600
So that's the thing with using the external variables in anonymous functions, the copy of this variable

07:46.600 --> 07:51.490
is created that only exists in the context of this function.

07:51.850 --> 07:56.470
So to actually be able to change the value of this variable.

07:56.470 --> 07:57.610
If you would want that.

07:57.610 --> 08:04.030
Obviously you need to pass this variable by reference using this end sign.

08:04.090 --> 08:05.530
And then.

08:07.870 --> 08:12.520
The change would also be made in the original variable.

08:13.420 --> 08:18.130
So at this point the reference might be a foreign concept to you.

08:18.130 --> 08:21.010
And I haven't explained it yet.

08:21.040 --> 08:27.820
So now that we have learned enough about basic data types and functions, I think we should go back

08:27.820 --> 08:36.850
to the variables and talk about the variables scope, because having to use this external variable is

08:36.850 --> 08:45.790
the direct results of variables existing in certain scopes, and also passing by value and passing by

08:45.790 --> 08:49.900
reference is something I would have to explain.

08:50.080 --> 08:52.450
Let's talk about that next.
