Functional Programming in PHP: Part I

Lochemem Bruno Michael
3 min readJul 28, 2017

I know, the title looks weird and somewhat unbelievable. PHP is not necessarily a language one would associate with organic functional programming support. If you read my post on the PHP RFC on immutable types, you will be able to internalize this fact. I have been writing PHP for two years now and have grown to accept its perks and flaws. Anyway, onto the crux of the matter which is, functional programming.

Fundamentally, functional programming is a programming paradigm premised on using functions. Functions are, according to this paradigm, at the epicenter of a code base. Implicit in this declaration is this: functional programming is, supported by any language with function support. PHP is one such programming language the latest versions of which allow for scalar type hinting and return type declaration. I highly recommend using PHP 7.0 or greater to enjoy the fullness of the functional programming experience.

PHP affords those who use it, the ability to create closures and anonymous functions (rejoice JavaScript aficionados).Anonymous functions look a lot like this:

$add = function (int $x, int $y) : int {
return $x + $y;
} //takes two integer inputs, x and y

Closures, an upgrade on anonymous functions with a closed scope, appear like this:

$add = function (int $x) use ($y) : int {
return $x + $y;
} //takes an external value, $y and adds it to $x

Closures use values defined outside their scope; they are convenient for interaction with the external state of their environment. The value y in the snippet above can be defined anywhere in your application.

The next item in this preamble is closures inside classes. Classes are predominantly associated with Object oriented programming but can be used in the functional programming space too. Classes are a requisite functor and monad requirement — concepts that will be discussed later in this functional programming series. Methods inside classes are easy to create; an example could be:

class simpleArithmetic
private $valX;

private $valY;

public function __construct(int $valX, int $valY)
$this->valX = $valX;
$this->valY = $valY;
public function add() : int
return $this->valX + $this->valY;
$add = (new SimpleArithmetic(12, 13))->add(); //returns 25

Notice how there is no setter in the above class. Setters are considered impure (more on this later). The add method builds on the values defined in the constructor and calculates the subtotal of the parameters $valX and $valY.

The final item on the agenda is higher order functions. A higher order function can either return a function or take a function as an argument for computation. Considering the duality of higher order functions, the two scenarios conveyed below should suffice to demonstrate their underlying concepts:

//function as argument
$stringManip = function (callable $fn, string $str) {
return $fn($str);
$stringManip("strtoupper", 'foo'); //should return FOO//function as returned result
$stringManipTwo = function ($str) {
return function ($fn) use ($str) {
return $fn($str);
$stringManipTwo('foo')('strtoupper'); //should return FOO as well

In conclusion, it is worth noting that the callable type is one specified to hint at a callback function. This type-hint, commonly associated with anonymous functions, references extensions of the PHP Closure class. The next part of the series will feature a demystification of function purity and an introduction to value objects.

The series — Functional Programming in PHP — is now a book. The volume eponymous with the series is currently available on Leanpub.