Functional Programming – How to Write Functional Code in PHP

Functional Programming – How to Write Functional Code in PHP

4 80320
Functional Programming - How to Write Functional Code in PHP

Functional Programming – How to Write Functional Code in PHP

Functional programming can be defined in simple terms as a programming paradigm that do not change the state of a program instead it uses pure functions. A pure function is a function that has the ability to accept a value and return another value without changing the input supplied to it. It is characterized by its ability to support functions that are of high order. This implies that these functions take and return other functions. Also functional programming supports systems that are elaborated in nature, that is, they can prevent the issue of null pointers that is a problem in object oriented programming. A programming paradigm that is functional has the following attributes: do not alter the states which make parallelism easier, deals mostly with a function which is the smallest unit hence enhances readability of code, has deterministic functions that enable stability of a program.


Functional Programming

How PHP implements functional programming

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

Anonymous functions or lambda functions

They are functions that use internal closure class during their implementation. In PHP 5.3.x 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. Basically they are classes that do not contain a name. The values that are seen by anonymous functions are those that were bound during function definition.

Closures

By definition 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. For example

$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

In PHP a partial function is derived from a general function that takes many variables in that a partial function has the capability of fixing most of the variables in a general function. It can be implemented using closures as follows

$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

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

Higher order functions

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

Pure functions, immutable data

It is not easy to implement this in PHP but to make it possible 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 alter the inputs and outputs.PHP also implements immutable data by defining its variables as constants.

Recursion

It is a technique 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 so as to maintain the state and prevent data mutation. Recursion is not implemented in a structured way rather by function calls.

Advantages

Functional programming has many advantages as far as PHP is concerned but not all advantages apply to PHP since its not a programming language that is functional in its design. Some of the advantages include:

  • Referencing: anonymous functions are used to reference factions that kept for future use.
  • Secure programs: this is achieved by using immutable data and maintaining the same state whereby a program is divided functions to make it manageable.
  • Code reuse: this is achieved through recursion whereby we use the results of an existing problem to solve another problem.

Disadvantages

  • The cost of programming: recursion comes with some internal overheads such as space and time that brings about efficiency problems.
  • It is not easy to learn functional programming as compared to imperative since it has tools that gives light to solve old problems.

In conclusion functional programming is just not only a programming paradigm since it takes into consideration the composition of functions and their arrangements without using specific steps explicitly. This calls for languages such as PHP to implement functional programming so as to solve commercial problems.

Author: Koushal is an eMarketing enthusiast who loves to help people in developing effective online marketing strategies and is working with ValueCoders, an IT outsourcing company that offers web development, software development, mobile application development and many more. One can hire PHP developer here for effective PHP development services.

SIMILAR ARTICLES


4 COMMENTS

Leave a Reply