Higher order functions are functions that either take functions as arguments or return functions.
In this case, apply is a higher order function because, as you may be noticed or not, this OP argument
is then called as a function which makes apply the higher order function.
So we can also add an arrow function like this and pass it to the call when we are calling apply, which
makes apply the higher order function.
Let's see another example.
So consider that we have this array.
We would like to sort the people by age.
We can use use sort for that passing this array.
And the second argument in this case is just an arrow function that is doing the sorting logic.
That's why you sought is an higher order function.
So function that either takes functions as arguments or returns a function.
And that's the result in this case.
So we've already covered higher order functions before.
If I now search for the Arraymap function inside the project, I can find it in the arrow functions
and the anonymous functions files.
So actually we've been using higher order functions without knowing that they are higher order functions.
So Arraymap is a good example.
And a lot of array related functions are also higher order functions.
So I don't think there is a point in rewriting the examples from the slides.
Instead, let's create a higher order function that returns a function.
So let's create a higher Order.
FBX file for this example.
PHP tag and let me put some data because I just don't want to waste your time typing this on the video.
So what I'd like to do with this array, this multi-dimensional array is from this main array, I only
want the elements that are under the key role will have an admin.
So at least that's the first filter that I would like to create.
But I would like to be also a little bit more flexible with what filters I can create.
So I'm not gonna make a specific function that will perform the array filtering for that key.
We're going to create a generic higher order function that we can then reuse.
However we want to filter for different properties.
So I'm creating a function I'm going to call it create filter.
And what it needs to know is which key I'm interested in and what needs to be value of that key.
So that's not a higher order function yet, but now it is because I am returning a function from it.
So this function for every item that it receives, it just needs to compare the item key with value.
That's a strict equality operator.
Now this might look complex.
Right now you might not understand what the hell I'm doing, but don't worry about that.
We're gonna run this function and I'm gonna explain it.
So first, since this is a higher order function which returns a new function, this means we first
need to call it with some parameters to get a function that only then we can use.
So that's the purpose here.
So I am creating a function that I'm going gonna call is admin.
Remember, functions are first class citizens in PHP.
They can be passed around as arguments, returned from functions, or assigned to variables.
That's why I can do that.
Now I'm calling create filter and take a look at the arguments that we have.
We've got the key and value.
Now those are the keys.
So I'd like the role key to be exactly an admin.
So I am creating a filtering function that does exactly this.
What I'm gonna do with it.
Well I'm gonna get the admins using the built in PHP array filter.
By the way.
There's also a higher order function, because one of the arguments here is a function, our filtering
function.
So the way it works is that for every element from the past array which is users this main array.
Every item will be passed to this function.
So how does our function look like?
Well, we've created it using this higher order function.
And it's gonna receive an item.
That's why when we refer to item key we actually refer.
This is for example an item.
And in this item that's a key and value.
Well that's this part.
So we should be getting only the users that are admins.
Let me squeeze that more into the screen.
So it's visible what's happening.
So finally I need to run that.
So let's do Vardump and let's just see who's the admin.
Let's open the terminal and run higher order PHP.
And the result is an array with only one item because only Alice is an admin.
I think that's correct.
Others are users.
Now we can create another example I can make another function called is Bob.
If for any reason I'd like to talk to everyone who's name is Bob.
So the name has to be Bob.
And I can just directly vardump that without using any additional variables.
So I do array filter on users and calling the is Bob function in this case.
Now let's run that.
And I expect in this second result to get one element inside this array which should match our requirements.
So the name should be Bob.