PHP Scripts – WordPress Tutorials

Functional programming is a programming paradigm that treats computation as the evaluation of mathematical functions and avoids state and mutable data. It emphasizes the application of functions, in contrast to the imperative programming style, which emphasizes changes in state. Functional programming has its roots in lambda calculus, a formal system developed in the 1930s to investigate computability, the Noninterchangeable, function definition, function application, and recursion. Many functional programming languages can be viewed as elaborations on the lambda calculus.

PHP has the following characteristics that make it support functional programming. They include:

Anonymous OR  lambda functions:

They are functions that use internal closure class during their implementation. In latest version of PHP they use the invoke() function. In PHP 5.4.x an anonymous function can be defined inside a class but to access the properties and methods of that class it uses variables like “$this”. Mainly they are classes that do not have a name. The values that are seen by anonymous functions are those that were bound during function definition.

Closures

By meaning a closure is similar to an anonymous function but the difference is that a closure encloses some parts of its surroundings (external scope). Since PHP uses early binding by default the values at the external scope can be seen by implementing late binding. This is done by passing by reference the variables outside the scope which is implemented applying the keyword use.

1
2
3
4
5
6
$variable = 10;
// Create a closure using a variable outside of the scope of the anonymous function.
$closure = function($arg) use ($variable)
{
Return $arg + $variable;
};

Partial functions and currying

Partial function is consequent from a general function that takes many variables. In that a partial function has the capacity of fixing most of the variables in a general function.

NOTE: Curry is not a PHP function, but it can be created.

It can be implemented using closures as follows

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
$general_function = function($a, $b){

Return $a * $a - $b * $b;

};

$partial_function = function ($a) use ($general_function) {

Return $general_function ($a, 5);

};

//Currying is a technique whereby a function that takes one argument can be created from a function that takes multiple arguments. Example:

$add2 = curry($add, 2); //returns a new function reference, to add(), bound with 2 as the first argument

$add2 (8); // return 10

 

Higher order functions

Higher order functions are functions that a able of accepting functions as their input parameters. The input function’s parameters must accept the same number of parameters and the output function takes the same number of parameters. Higher order functions are implemented by closures and lambdas.

Pure functions, immutable data

It is not effortless to apply this in PHP but to make it feasible one has to avoid state and mutable data.PHP implements pure functions by using variables such as global or static but these variables do not change the inputs and outputs.PHP also implements immutable (unchallengeable) data by defining its variables as constants.

Recursion

It is a procedure in programming whereby the output of a problem relies on the outputs of other smaller problems but the smaller problems must be from the same problem. Recursion is used to maintain the state and avoid data mutation. Recursion is not implemented in a structured way rather by function calls.

Random Posts


Categories: PHP