Can PHP be a functional programming language?

Can PHP be a functional programming language?

I recently remembered a library by my former team lead and good friend Slava Basko that lets you write PHP code in a functional style. This article is about my experience getting to know it, my reflections and a comparison with the approaches I use in my everyday practice.

Those who know me know about my love for OOP, architectural purity and tight control of dependencies. In fact, I became like this largely thanks to Slava — he was the one who once opened the world of OOP to me. But years have passed, and Slava got into Haskell and the functional approach to programming in general. And here's the result — he created a library that brings the functional approach to the PHP world.

What's the point of the library?

The library lets you:

  • write declarative code without intermediate variables and imperative constructs;
  • use currying, pipe, compose, partial and so on;
  • build readable transformation chains without losing context.

An example — summing up the quantity of products in an array:

$totalQty = compose(sum, pluck('qty'))($products);

Another example — formatting product descriptions:

$commonDescription = pipe(
    pluck('description'),
    map(unary('trim')),
    select(unary('strlen')),
    join(', '),
    take(34),
    partial_r('trim', ', ')
)($products);

Comparing the functional approach with OOP and the procedural approach

PP — procedural programming, OOP — object-oriented, FP — functional.

PP OOP FP
Readability Straightforward, but with lots of variables Structured, but often too verbose Concise, but may feel unusual
Reuse Hard to scale Abstractions, inheritance Composition, closures
Testing Depends on global state Often needs mocks Simple thanks to pure functions
Flexibility Low Depends on the developer's skill, medium or high High for transformations
Dependency control None Full Partially possible, much harder to track, or I haven't figured it out
When is it convenient? If you process arrays or data structures If you have many transformation steps If you want to get rid of variables and foreach

Conclusion

This is not an approach most PHP developers are used to. But it works, it's convenient, and I will definitely use it inside class methods where data needs to be transformed quickly and clearly. Pure functions and pipe fit perfectly for processing DTOs, validation, generating descriptions and so on.

Worth a look: slava-basko/functional-php.


First published in Ukrainian in the Telegram channel “Created to die” on June 20, 2025.