Thinking Functionally in PHP
With Membership
Suggested price

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.


About the Editor

Larry Garfield
Larry Garfield

Larry Garfield is an aspiring blacksmith who moonlights as a Staff Engineer for the LegalZoom. He has been an active part of the PHP community for many years, in a variety of roles formal and informal.  When not trying to hand-forge his own medieval armory from scratch he tries to teach developers and development managers the skills of yesteryear that the industry has forgotten.

Reader Testimonials

Dmitri Goosens
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
Dan Blows

Most succinct monad description

Probably the first time I’ve understood monads and currying in 10 years of trying!

Steve McDougall
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
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
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.


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
  • 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
  • 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
  • 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

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...

80% Royalties. Earn $16 on a $20 book.

We pay 80% royalties. That's not a typo: you earn $16 on a $20 sale. If we sell 5000 non-refunded copies of your book or course 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

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.

Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub