Functional programming
Functional paradigm#
Functional programming is one of three available programming paradigms (the other two are structural and object-oriented programming). It was created before the first computer program was written. It is a direct consequence of the work of Alonzo Church, who invented the lambda calculus.
The fundamental characteristic of the lambda account is immutability. No value or symbol can change.
This one feature makes the functional approach lacking assignment instructions. In fact, it is available, but it is nevertheless subject to very strict discipline.
Functional programming in PHP#
PHP supports classic concepts from the functional world, such as:
- first-class function (function can be assigned to a variable, can be referenced by a variable and invoked dynamically)
- recursion (allows a function to call itself)
- anonymous functions (with support for closures and ability to bind closures to an object’s scope)
- higher-order functions (such as
array_filterorarray_map)
Below we can see a simple example of using several of them:
Unfortunately, despite this, there is no common interface to seamlessly connect all aspects of the functional world. There are also other problems:
- not every function guarantees immutability (
array_mapvssort) - difference in order of parameters (
array_filtervsarray_map) - lack of fluent interface (
array_*hell)
Munus tries to solve these and other problems so that you can enjoy the joy of functional programming in a fully object oriented style.