Functional Programming in PHP: Higher-order Functions

Functional Programming in PHP: Higher-order Functions

If you examine several frameworks and large-scale applications, you’ll certainly see a higher-order function at some point. Many languages support the idea of higher-order functions, including JavaScript, Java, .NET, Python and even PHP, to name a few.

But what is a higher-order function and why would we want to use one? What advantages does it give us and can we use it to simplify our code? In this article, we’ll talk about higher-order functions in PHP specifically, but will show how they’ve been used in other languages for comparison.

First-class Functions

Before we can get into what a higher-order function is, we must first understand that we have to have a language that can support a feature called first-class functions (aka first-order functions). This means that the language treats functions as first-class citizens. In other words, the language treats functions like it does variables. You can store a function in a variable, pass it to other functions as a variable, return them from functions like variables and even store them in data structures like arrays or object properties like you can with variables. Most modern languages these days have this feature by default. All you really need to know is that a function can be passed around and used much like variables are.

For our purposes, we’ll be focusing mostly on passing functions as arguments and returning functions as results, and briefly touching on the concept of non-local variables and closures. (You can read more about these concepts in sections 1.1, 1.4 and 1.3 of the Wikipedia article that was linked to in the previous paragraph.)

What Are Higher-order Functions?

There are two main characteristics that identify a higher-order function. A higher-order function can implement just one or both of the following ideas: a function that takes one or more functions as an input or returns a function as an output. In PHP there’s a keyword that’s a clear giveaway that a function is higher-order: the keyword callable. While this keyword doesn’t have to be present, the keyword makes it easy to identify them. If you see a function or method that has a callable parameter, it means that it takes a function as input. Another easy sign is if you see a function return a function using its return statement. The return statement might be just the name of the function, or could even be an anonymous/in-line function. Below are some examples of each type.


function echoHelloWorld() 
  echo "Hello World!";



function higherOrderFunction(callable $func) 
  $func();





higherOrderFunction('echoHelloWorld'); 

Here are some simple examples of higher-order functions that return a function:


function trimMessage1() 
  return 'trim';



function trimMessage2() 
    return function($text) 
        return trim($text);
    ;



$trim1 = trimMessage1(); 


echo $trim1('  hello world  ');


$trim2 = trimMessage1(); 


echo $trim2('  hello world  ');

As you can imagine,

Read More