October 13th, 2024

Functional PHP (2015)

The author advocates for using functional programming in PHP, highlighting benefits like improved modularity, reduced complexity, easier debugging, and enhanced maintainability through small, single-purpose functions and higher-order functions.

Read original articleLink Icon
Functional PHP (2015)

The article discusses the author's experience with writing PHP code in a functional programming style. The author found this approach beneficial, as it allowed for clear separation of responsibilities through the use of small, single-purpose functions. By utilizing functional programming concepts such as immutability and higher-order functions like `map`, `reduce`, and `filter`, the code became more modular and easier to maintain. The author noted a significant reduction in errors and complexity, with a maintainability index that improved with the addition of comments. The functional style facilitated easier modifications and debugging, making it simpler to identify and fix issues. Overall, the author advocates for the functional programming approach in PHP, highlighting its advantages in creating clean, efficient, and testable code.

- The author successfully implemented functional programming principles in PHP.

- The approach led to improved code modularity and maintainability.

- The use of small, single-purpose functions reduced complexity and errors.

- The functional style made debugging and modifications easier.

- The author encourages the adoption of functional programming in PHP development.

Link Icon 6 comments
By @sourcecodeplz - 7 months
I've tried Node when the trend started and it was okay. But the problem was that the actual script was also the server. So if the script hanged, the whole server hanged. Went back to PHP and never looked back.
By @skrebbel - 7 months
(2015)

Btw while this code doesn't win a prize, I agree with the author that functional style is worth it in many cases, even for a language as wordy and cumbersome as PHP. I still love PHP - its deployment story is still unparalleled and its standard library came batteries included since forever.

By @huqedato - 7 months
writing/thinking in FP style since 2020. Elixir, Js, F#, Julia and even PhP. Never got back to OOP. Never needed to. I realized what a waste of time and frustration had been OOP for me for almost 15 years (Java, C#).
By @cies - 7 months
You can write OOP-style in C, FP-style in C/C++/Java or even PHP. Sure.

Writing pure FP code (pushing all the side effects to the "edges of the program" in C is possible! But it's going to take a lot of discipline from the programmers: there is no safety-net and lots of dreadful boilerplate.

Impracticle, but possible.

By @chx - 7 months
tl;dr: Hardware is cheap. Engineers are expensive.

> Instead of fearing the overhead in PHP for function calls,

Already in 2015 in almost all applications this is a premature optimization which is rightly known as the root of all evil. Please. Your app will talk to the network, likely run a database query which is like thousands or millions of times slower than a function call. Further, even if there is a measurable difference the cost of hardware which makes that difference go away is very likely to be smaller and much smaller at that than the cost of the engineering hours wasted during maintenance when wrestling with code which was written with a "function calls are expensive" mindset.

This doesn't mean you don't need to worry about performance and scalability but even that is going to be much easier if you have a well structured code.

By @cynicalsecurity - 7 months
His code is crap, I'm not even sorry for saying this. For the case he is working with, OOP is absolute must.