Functional programming isnt something that is often associated with PHP. Yet for quite a while PHP has had all the features necessary to create software using the functional paradigm. In this book, youll take a look at what functional programming is, how to do it in PHP, and the different ways in which you can use it to improve your PHP software.
Who Is This Book For?
This book isnt an introduction to PHP itself; it assumes you have some basic (or, indeed, advanced) experience in PHP scripting. You dont need to be an expert to follow along; Ill cover all the key concepts in PHP youll need to know to be able to implement functional designs in your code and point you in the direction of resources such as web sites and other books that you can use to learn or investigate any related concepts that I dont cover directly.
Absolute PHP beginners aside, this book is suitable for all programmers. Whether you have a pressing need to learn functional programming (perhaps youve taken over a functional PHP code base) or you are just interested in finding out what the buzz around functional programming is all about, there is something in this book for you. Theres even likely to be something for those skeptical about creating software using the functional programming paradigm. I think that most programmers will find useful lessons and code patterns to take away from the functional programming style that will enhance their object-oriented or procedural programming work. If all else fails, knowledge of functional programming looks good on your rsum!
What Is Functional Programming ?
Functional programming is a declarative programming paradigm that abstracts code into pure, immutable, side-effect-free functions, allowing the programmer to compose such functions together to make programs that are easy to reason about.
That is my definition of functional programming. Ask five other functional programmers to define functional programming and youll get four more answers (two just copied the same answer from Wikipedia). Theres no standard definition; different people and different programming languages implement functional programming elements differently. These differences are partly because of the practicalities of the language in question and sometimes because of the target platforms, data, and usage scenarios, but often they come down to what I call programming religion: a fixed, sometimes irrational, but often deeply held belief of how a particular paradigm should be . Even within the small community of PHP functional programmers, you wont find an exact consensus. In PHP, functional programming is not a core concept, but even in languages where it is (e.g., Lisp, Scala, etc.), there are many related understandings of what constitutes true functional programming. While that may sound problematic, youll still know it when you see it, and when it gets woolly around the edges, you can choose to define it in any way you see fit!
PHP isnt a pure functional programming language, but you can still use it for functional programming (which is good; otherwise this book wouldnt be very long). A few elements of what some purists consider to be essential functional programming concepts are harder to implement with PHPs standard syntax, so its perhaps slightly more accurate to say that you can program in a functional programming style in PHP.
Lets now look a little more in depth at what functional programming actually is in practice. Functional programming is a declarative style of programming, which means you specify what you want it to do rather than how you want to do it. Its a higher level of abstraction than you may be used to with OO or procedural programming. However, you almost certainly use declarative programming on a day-to-day basis when using SQL, HTML, regular expressions, and similar languages. Consider the SQL snippet shown in Listing .
SELECT forename,
Surname
FROM users
WHERE username = 'rob'
AND password = 'password1';
Listing 1-1.
declarative.sql
This is telling your database server what you want it to do (select the real name based on super-secret security credentials), but you dont tell it how to do it. You dont tell it the following:
Where to look on disk for the data
How to parse or search the data for matching records
How to determine whether a record matches your criteria
How to extract the relevant fields from the record
And so on. You simply tell it what you want it to achieve for you.
Now obviously, at some point, you need to tell the computer how to do something. With the SQL example in Listing , you do that by getting some rather clever people to write database management software (DBMS) for you. In functional programming, youll tend to need to write the implementation code yourself, but to make it a manageable task, you break that down into the smallest possible chunks and then use a hierarchical chain of declarative function calls to tell the computer what to do with that code. If you use the Composer dependency management system, you will already be using a similar paradigm: there are many libraries of code available that abstract away the tasks that you need to do; you simply compose a list of libraries together to do what you want. In functional programming, you do exactly the same; you take functions that do something (like the libraries Composer provides) and compose them together into a program.
Having a program that is essentially a list of what you want to achieve sounds very good on paper, and indeed it makes it easy to understand and reason about your program. To make the idea a little more concrete, lets take a look at a small functional-style program (Listing ).