On the Liip blog today there’s a tutorial from Gilles Crettenand giving you an overview of functional programming in PHP. While PHP is not normally used as a functional language, it is possible to simulate the same effect.
Functional programming has gained a lot of traction those 3 to 5 last years. […] Those [frameworks and languages] are all cool and shiny new toys, but we can benefit from some techniques without having to learn a new tool, just by applying some principles to our everyday PHP! But first of all, what exactly is functional programing?
He starts off with some of the basics of functional programming, some of the difficulties that can come with it and, of course, the advantages it can provide. From here he starts in with code examples. He shows how functions become „first-class citizens“ and how they can be applied to various elements. He illustrates this with a few array manipulation examples. Next up are „utility functions“ for evaluating the data given (like „any“ or „all“). He ends the post looking at the idea of „memoization“, or the caching of the results of function calls against data. He shows how to accomplish this with static local variables in PHP and includes a wrapper you can pass any callable function into and have the results cache automatically.