Thinking Functionally in PHP
Thinking Functionally in PHP
About the Book
Functional programming has a reputation of being too nerdy, academic, and inscrutable to be useful for "common" programmers. That reputation is only partially unearned, but the concepts behind functional programming are entirely within reach of every programmer in any modern language. That includes PHP. This book takes the reader through the basics of functional programming, the new capabilities of PHP 7.4 that make it even easier, and the computer-science-y underpinnings that make it all possible.
Computer scientists and software engineers tend to speak in entirely different languages, which is part of what makes learning more academic concepts difficult. This book offers both: Practical, applicable examples of how to leverage functional programming, category theory, and even the scary m-word. We start with the very basics and build up slowly, covering both the practical and theoretical just enough to be useful in everyday code.
Translations
Reader Testimonials
Dmitri Goosens
Monads without headaches
The man is a genius He managed to explain a #monad in ten lines or less without any headaches. He deserves the Pulitzer for tech books for that.
Dan Blows
Most succinct monad description
Probably the first time I’ve understood monads and currying in 10 years of trying!
Steve McDougall
More than educational, an inspiring thought provoking read
Thinking Functionally was a book I was unsure of to begin with. Having used functional programming languages before I wasn't sure how much benefit I would get from it. As it turns out it has completely changed my perspective and approach to not just functional programming, but programming in general - a must read for anyone wanting to understand that bit more than they already do.
Matthew Boswell
The most approachable book on the subject
This book is the most approachable on the subject I have found. It offers practical examples but does not skimp on the theoretical knowledge that helps apply functional concepts in the wild. The ideas are easily applied regardless of the programming language.
Marian Kostadinov
Earlier today I realized that several months after reading this, book my PHP codebase is free of stateful mutable objects (except for some storage and cache) and it is so much easier to use and manage with functional techniques in mind.
@giyoomu
Better monad explanation than Haskell
Your introduction to theory category is nice. I have probably understood it more by reading your book than tinkering with Haskell during months.
Table of Contents
-
- Preface
-
Introduction
- Functional vs. Procedural
- Object-Oriented Code
- Functional Style? Functional Language?
- Type Systems
- What to Expect
-
Part One
-
Pure Functions
- Define “Pure”
- Composing Functions
- Binary Functions
-
First Class Functions
- Anonymous Functions
- Closure Objects
- Short Lambdas
- Object Binding
- Memoization
- Currying
- Mapping
- Filter
- Reducing
- Concatenation
-
Recursion
- Recursion
-
Immutable Value Objects
- What PHP Does
- Making It Immutable
- Objects, Functionally
- Evolvable Objects
- Dependencies
- Entity Objects
- Intra-Function Immutability
-
Pure Functions
-
Part Two
-
Category Theory
- What Is a Category?
- Objects
- Some Definitions
- Structure
- Let’s Get Meta
- Isomorphism
- Monoids
-
Categories in Code
- Converting Structure
- Function Objects
-
Fun With Functors
- Combining Functors
- Monads in Programming
- Anatomy of a PHP Monad
-
Algebraic Data Types
- Products
- Product Types in PHP
- Coproducts
- Coproducts in PHP
-
Category Theory
-
Part Three
-
Handling Null
- The Problem
- The Example
- The Category Theory Solution
- The Programming Solution
- An Alternate Implementation
- A Note on Types
-
Either/Or and Error Handling
- The Problem
- The Example
- The Category Theory Solution
- The Programming Solution
- Either Either?
-
Tracking Data
- The Problem
- The Example
- The Category Theory Solution
- The Programming Solution
-
Combining Monadic Behavior
- The Example
- The General Approach
- Binding
- The Lexers
- Testing
- Analysis
- Being Lazy
-
Handling Null
-
Part Four
-
Features of Functional Languages
- Auto-Currying
- Recursion Optimization
- Isomorphic Optimization
- Function Composition
- Function Naming
- Generics
- Callable Types
- Comprehensions
- Integrated Optionals
- Optional Chaining
- A Native Bind Operator
- Enumerated Types
-
When to Go Functional
- Avoid Global State
- Make Pure Functions
- Isolate IO
- List Application
- Embrace Value Objects
- Embrace Functions as Values
- Make Single-Method Callable Services
- Think in Terms of Pipelines and Data Flow
- Use Maybe and Either for Error Handling
- Don’t Overdo It
-
Further Resources
- Books
- Videos
- Papers
-
Features of Functional Languages
Other books by this author
The Leanpub 60 Day 100% Happiness Guarantee
Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.
Now, this is technically risky for us, since you'll have the book or course files either way. But we're so confident in our products and services, and in our authors and readers, that we're happy to offer a full money back guarantee for everything we sell.
You can only find out how good something is by trying it, and because of our 100% money back guarantee there's literally no risk to do so!
So, there's no reason not to click the Add to Cart button, is there?
See full terms...
Earn $8 on a $10 Purchase, and $16 on a $20 Purchase
We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.
(Yes, some authors have already earned much more than that on Leanpub.)
In fact, authors have earnedover $13 millionwriting, publishing and selling on Leanpub.
Learn more about writing on Leanpub
Free Updates. DRM Free.
If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).
Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.
Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.
Learn more about Leanpub's ebook formats and where to read them