Tag Archives: functional

Functional programming in PHP

With the introduction of lambda (or anonymous) functions in PHP 5.3 it is now possible to introduce some nice features from the functional programming paradigm.

Higher order function

When a method (or function) accepts another function as parameter this function is called a higher order function. The following already worked in PHP < 5.3 and is a simple implementation of functional programming with callable variables.

$data = array(10,2,6,4);
function cmp($a, $b) {
    if ($a == $b) return 0;
    return $a < $b ? -1 : 1;
}
usort($data, "cmp");

Continue reading Functional programming in PHP