Can PHP be a functional programming language?

← Can PHP be a functional programming language?

Ashterix

docs: English JsonRpcBundle docs, REST and MCP Bundle sections, package cards

en/articles/from-tg/php/functional-php.md new file +56 βˆ’0

1 + # Can PHP be a functional programming language?
2 +
3 + I recently remembered a [library](https://packagist.org/packages/slava-basko/functional-php) 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.
4 +
5 + 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.
6 +
7 + ## What's the point of the library?
8 +
9 + The library lets you:
10 +
11 + - write declarative code without intermediate variables and imperative constructs;
12 + - use currying, pipe, compose, partial and so on;
13 + - build readable transformation chains without losing context.
14 +
15 + An example β€” summing up the quantity of products in an array:
16 +
17 + ```php
18 + $totalQty = compose(sum, pluck('qty'))($products);
19 + ```
20 +
21 + Another example β€” formatting product descriptions:
22 +
23 + ```php
24 + $commonDescription = pipe(
25 + pluck('description'),
26 + map(unary('trim')),
27 + select(unary('strlen')),
28 + join(', '),
29 + take(34),
30 + partial_r('trim', ', ')
31 + )($products);
32 + ```
33 +
34 + ## Comparing the functional approach with OOP and the procedural approach
35 +
36 + PP β€” procedural programming, OOP β€” object-oriented, FP β€” functional.
37 +
38 + | | PP | OOP | FP |
39 + | --- | --- | --- | --- |
40 + | **Readability** | Straightforward, but with lots of variables | Structured, but often too verbose | Concise, but may feel unusual |
41 + | **Reuse** | Hard to scale | Abstractions, inheritance | Composition, closures |
42 + | **Testing** | Depends on global state | Often needs mocks | Simple thanks to pure functions |
43 + | **Flexibility** | Low | Depends on the developer's skill, medium or high | High for transformations |
44 + | **Dependency control** | None | Full | Partially possible, much harder to track, or I haven't figured it out |
45 + | **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 |
46 +
47 + ## Conclusion
48 +
49 + > 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.
50 + > Pure functions and pipe fit perfectly for processing DTOs, validation, generating descriptions and so on.
51 +
52 + Worth a look: [slava-basko/functional-php](https://packagist.org/packages/slava-basko/functional-php).
53 +
54 + ---
55 +
56 + *First published in Ukrainian in the Telegram channel [β€œCreated to die”](https://t.me/php_while_true/20) on June 20, 2025.*