Function Composition in PHP
Learn about function composition and its various styles.
Function composition
The practice of chaining function calls, or composition, is common in functional programming. In fact, the spirit of functional programming is composition. A function chain is a nest of functions and, like any series, follows a linear chronology. In a function chain, the output of one function serves as input for the next. This concept is rooted in mathematics, where two functions, and , can be composed to create a new function, , or .
Function composition in PHP can be approached in one of two ways:
- Traditional style
- Point-free style
The first style is quite popular and reflects a composition one may not be aware of at first. The second style enforces more compact function definitions and more recognizable chains.
Get hands-on with 1400+ tech skills courses.