Anonymous Function and Callback problem in PHP

Tram Ho

Callback () ??? What you still hear (or read) in Javascript tutorials is now also available in PHP. Well actually, it dates back to the Song Dynasty, so if anyone knows, I’ll kick you lightly 😄

For those of you who have not been in PHP for a long time, you may not be familiar with this concept or even believe it exists, until you work with Laravel for a while and you still do not realize you have been use it?! So look at the following function:

Look familiar? The local function ($query) ... is called a callback function of whereHas()

To finally understand what’s cool about callback() in PHP, start learning about

Anonymous Function

Like the title, this is the origin of the callback or in short: a function callback is an Anonymouse Function. As the name implies, this is an anonymous function, that is, it has no name but is only defined as a function and will perform something.

This is an anonymous function, that is, a function is declared as normal

And the usage is also normal 😐

Anonymouse Function is basically a normal function, it can also take a parameter and return a certain result, except they are defined without a name, for example:

However, there are 2 points to note, the first is that this function has no name (obviously 😐 ), the second end is always a semicolon after the last opening parenthesis.

But which type to use? Because when we want to use a function, we will call its name, and the function without a name will call in the eyes ???

Assign variables to Anonymous Function

AF (Anonymous Function) can be assigned to any variable, this variable can then be called similar to a function or can even be pushed into an array, and so we have an array of functions always, quite Useful in many cases that I haven’t thought of: 3

Or even create an array of functions

Create a Closure (What we will learn in another article)

Finally the most important thing, do a functioin callback

Callback Function

This is almost the most important function of an AF, for you who do not know what is a callback, it is a function that is put into another function as a parameter and this function can use the local scope in function that it is to include.

Take a look at the example below:

In this example, the second argument to the main showText function as a Callback function is written as an Anonymous Function. Some of you might be wondering that in the example above, how does the callback work?

Explain:

  • The first time when showText is called, its first argument, 'Tony Stark' is put under the $string variable, $string is put in the $result array to be processed.
  • If there is a second parameter passed, if will check if it is a function or not, if so, call call_user_func to bind local scope $result to the $name parameter of the callback.
  • Any time we have $name is $result and can call it as usual.

As you can see, a callback in PHP is basically quite similar to JS and you have dozens of uses with it (which you still use every day) that you can think of, or otherwise PHP defined a bunch of functions you may not know about: v

array_map ()

array_map() accepts the parameter passed as a callback function and the second parameter is an array, allowing the callback function that you pass to handle something with the array:

array_filter ()

Similarly, the function also accepts an array and a callback, and you can filter the array by a condition that you define in the callback, instead of using foreach and if

And of course what you see a lot in Laravel: collect()->map(function() { ... }) and collect()->filter(function () { ... }) also from 2 function on that out.

Rambling

The question I asked myself was, could the callback in PHP be hell like in JS ??

My personal opinion is not, because callbacks in JS are used a lot, especially when the code is handled asynchronously poorly. As for PHP, a language is synchronized from top to bottom so this will be unlikely if not impossible.

What about your opinions? Let me know in the comments, thanks for watching!

Share the news now

Source : Viblo