Computer Programming web Web programming Tips



By Sergey Skudaev


Since PHP version 5.3, PHP support functional programming. What does it mean? We could create function in PHP before. What is the difference? The difference is that now we can assign function to a variable or pass it as a parameter to a new function.

Let's look at the following eample of PHP code that converts Fahrenheit temperature to Celsius.


// function-variable that return value
function getFr(){

return 95;

}

// create ananimous (Lambda) function converting F to C

function($f){

//converting Fahrenheit to Celsius and
//rounding result to a whole number

return (round(($f-32)*(5/9),0));

};


Since the anonymous function does not have a name, to call it we have to assign it to a variable. A function that can be assigned to a variable is called Lambda function.

$C = function($f){

return (round(($f-32)*(5/9),0));

};

//Call function assigned to $C
echo '$C(getFr());='. $C(getFr());

?>

OUTPUT:  $C(getFr());=35

We can modify the variable function in the following way:


function getF($t){

return $t;

}

// we can assign the function to a variable:
$F = getF(90);

echo $F."<br>"; // output: 32

//Again creating a lambda function:

$C = function($f){

return (round(($f-32)*(5/9),0));

};

//Call $C and pass $F variable.
echo $C($F);

OUTPUT: 35

//Call $C passing getF() function as a parameter

echo $C(getF(95));

OUTPUT: 35


What is a closure? Closure is a function that can access variables outside the scope in which it was created.

For example, function convert($f) from the folowing example returns an anonymous function and that anonymous function can access $f variable that exists outside the scope of that anonymous function.


$F=90;

// Create a Closure
function convert($f) {
              return function() use ($f) {
                  echo (round(($f-32)*(5/9),0));
              };
}

$converter = convert($F);

$converter();

OUTPUT: 32


Another closure example. An anonymous function assigned to $convertToC variable accessing $n variable declared outside the function scope.


// Create a number
$n = 32;

// Create a Closure
$convertToC = function($ft) use ($n) {
  return (($ft - $n) * 5/9);
};

echo $convertToC(95); //

OUTPUT: 35


The next example demonstrates using partial function. In PHP a partial function is originated from a function with multiple parameters.

Original function:


	$cube = function ($w, $h, $l) {

	    return $w * $h* $l;

	};

A Partial function:


	$partial_function1 = function ($width) use ($cube) {

	    return $cube ($width, 3, 5);

	};

echo $partial_function1(2);

OUTPUT: 30


In previous example a partion function took one paramatar of three. It is possible to use partial function that takes 2 or all three paramaters.


$partial_function2 = function ($w, $h) use ($cube) {

	    return $cube ($w, $h, 7);

	};

echo $partial_function2(2, 3);

OUTPUT: 42


The partial function example with all three paramaters.


$partial_function3 = function ($a, $b, $c) use ($cube) {

	    return $cube ($a, $b, $c);

	};

echo $partial_function3(2, 3, 9);

OUTPUT: 54


Lambda functions and closures are new for PHP, but they are widely used in JavaScript, specially in jQuery for a long time. These new PHP features offer developers even more flexibility.

My eBooks on Amazon.com

US    UK    BR    CA
US   UK   BR   CA
US    UK    BR    CA