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

NASA closer to entire Hubble Area Telescope functions following computer system shut down

NASA closer to entire Hubble Area Telescope functions following computer system shut down

(NASA) – The US space application is continuing to convey the Hubble House Telescope again to standard science operations, most lately recovering the Broad Area Camera 3 instrument Sunday, Nov. 21. This camera will be the next of Hubble’s instruments, after the Superior Camera for Surveys, to resume science following suspending the spacecraft’s observations Oct. 25. The Vast Field Digicam 3’s initial science observation considering the fact that the anomaly will be Nov. 23.

The workforce chose to restore the most closely utilized Hubble instrument, the Wide Subject Camera 3, which represents a lot more than a third of the spacecraft’s observing time. Engineers also started getting ready improvements to the instrument parameters, when screening the modifications on ground simulators. These improvements would permit the devices to deal with many skipped synchronization messages when continuing to work normally if they manifest in the long term. These adjustments will to start with be utilized to one more instrument, the Cosmic Origins Spectrograph, to additional secure its sensitive considerably-ultraviolet detector. It will choose the group various months to comprehensive the tests and add the improvements to the spacecraft.

Despite the fact that the workforce has discovered no further more message losses considering the fact that monitoring commenced Nov. 1, NASA is taking more methods to hold the components harmless in scenario the problem reoccurs. Investigation carries on into the result in of the skipped messages. The remaining Hubble devices are nevertheless in safe and sound method and the relaxation of the spacecraft carries on to run as expected.


Nov. 16, 2021 — NASA’s Future Ways to Return Hubble Instruments to Usual Operation Status

The Hubble Place Telescope’s Highly developed Digicam for Surveys has continued collecting science knowledge as NASA works to address the anomaly that began Oct 25. Skipped synchronization messages halted science creation at that time, and the workforce has determined no supplemental missed messages considering the fact that monitoring began Nov. 1.

Hubble’s more instruments have remained in harmless manner whilst NASA normally takes actions to recover them to operational status. The relaxation of the telescope is working as envisioned.

Throughout the past week, the Hubble group has determined in the vicinity of-expression alterations that could be built to how the devices monitor and respond to missed synchronization messages, as properly as to how the payload computer screens the instruments. This would allow science functions to carry on even if numerous missed messages arise. The team has also continued examining the instrument flight software package to verify that all achievable methods would be safe and sound for the devices.

This approaching 7 days, the team will start off to determine the order to recuperate the remaining devices, including schedules for switching the instrument parameters just before screening and developing the techniques. They also will test these alterations to assure they perform as prepared though continuing to isolate the root bring about of the mistake.

The group expects it will take many weeks to complete these pursuits for the initial

Read More