Use Arrow Function in PHP with examples- 9 mins
It is available since PHP version 7.4 but many do not understand how to use it.
Arrow functions, also known as “
short closures”, were introduced in PHP 7.4 as a more concise way to define anonymous functions. They are particularly useful for small, simple functions.
Probably if you come from other languages you are already used to using them but if this is not your case I encourage you to continue reading the article where I show you some examples of use.
How to use Arrow Function
Arrow functions are defined using the
fn keyword, followed by the function parameters, the arrow (
=>) symbol, and the function body.
Here we can see a basic example of how to implement it.
Arrow functions can also capture variables from the surrounding scope, unlike traditional closures, arrow functions do not need to be explicitly defined variables from scope.
Now that we have seen how to define an arrow function in PHP, let’s see some examples of how to use them.
In the examples we can see that both the traditional anonymous function and the arrow function achieve the same result.
Using Arrow Functions in Array Map
They square each number in the input array using the
“Applies the callback to the elements of the given arrays”
Using Arrow Functions with Array Filter
We filter all the even numbers of an array using the
Filters elements of an array using a callback function
“Filters elements of an array using a callback function”
$callback= null, int
$mode= 0): array
Using Arrow Functions with Array Reduce
We sum all values in array with callback function using the
“Iteratively reduce the array to a single value using a callback function”
$initial= null): mixed
Using Arrow Functions with usort
Arrow functions can be used in various contexts, including callbacks for array by reference functions like
usort. Sort the array by age.
“Sort an array by values using a user-defined comparison function”
I think that with these examples we can see how to implement the arrow functions in the most common cases.
If you think that I have left some or you have some doubt about some case, leave me a comment :-)
Arrow functions were introduced in PHP 7.4 as a more concise way to define anonymous functions. They offer several advantages, but also have some limitations. Here are the pros and cons of using arrow functions in PHP:
- Concise Syntax : Arrow functions have a more compact syntax compared to traditional closures, which can make your code cleaner and more readable, especially for simple operations.
- Less Boilerplate : Arrow functions eliminate the need for writing the function keyword and curly braces, reducing the amount of boilerplate code.
- Implicit Return : Arrow functions have an implicit return, meaning that the result of the expression is automatically returned without needing a return statement.
- Short-lived Functions : Arrow functions are particularly useful for short-lived functions used in higher-order functions like array functions (e.g., array_map, array_filter, array_reduce) and callbacks.
- Limited Scope : Arrow functions have a limited scope and cannot be used to modify variables outside their scope. They don’t support the use keyword to capture variables from the surrounding scope.
- Single Expression : Arrow functions are limited to a single expression. They cannot contain multiple statements or complex logic. For more complex functions, you’ll still need to use traditional closures.
- Readability for Complex Logic : While arrow functions can improve code readability for simple operations, they might make more complex logic harder to understand. Traditional closures might be more appropriate in such cases.
- Compatibility : Arrow functions were introduced in PHP 7.4. If you need to maintain compatibility with older PHP versions, you cannot use arrow functions.
Choosing Between Arrow Functions and Traditional Closures:
- Use arrow functions for simple operations that fit within a single expression and don’t require modifying variables from the surrounding scope.
- Use traditional closures when you need more complex logic, multiple statements, or need to capture variables from the surrounding scope.
- Consider the overall readability and maintainability of your code when deciding whether to use arrow functions or traditional closures.
In summary, arrow functions can be a powerful tool for writing concise and readable code for simple operations. However, it’s important to be aware of their limitations and choose the appropriate tool based on the specific context and complexity of your code.
You can read the article on Medium