Leanpub Header

Skip to main content

Thinking Functionally in PHP

Approaching PHP from a functional perspective. Yes, really. (With just a pinch of category theory.)

Free With Membership

With Membership

Free!

$35.00

You pay

$35.00

Author earns

$28.00
$

...Or Buy With Credits!

You can get credits monthly with a Reader Membership
PDF
EPUB
WEB
475
Readers
About

About

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.

Share this book

Author

About the Author

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.

Leanpub Podcast

Episode 173

An Interview with Larry Garfield

Translations

Translations

Languages

Contents

Table of Contents

Preface

Introduction

  1. Functional vs. Procedural
  2. Object-Oriented Code
  3. Functional Style? Functional Language?
  4. Type Systems
  5. What to Expect
  6. Part One

Pure Functions

  1. Define “Pure”
  2. Composing Functions
  3. Binary Functions

First Class Functions

  1. Anonymous Functions
  2. Closure Objects
  3. Short Lambdas
  4. Object Binding

Memoization

Currying

Mapping

Filter

Reducing

Concatenation

Recursion

  1. Recursion

Immutable Value Objects

  1. What PHP Does
  2. Making It Immutable
  3. Objects, Functionally
  4. Evolvable Objects
  5. Dependencies
  6. Entity Objects
  7. Intra-Function Immutability
  8. Part Two

Category Theory

  1. What Is a Category?
  2. Objects
  3. Some Definitions
  4. Structure
  5. Let’s Get Meta
  6. Isomorphism
  7. Monoids

Categories in Code

  1. Converting Structure
  2. Function Objects

Fun With Functors

  1. Combining Functors
  2. Monads in Programming
  3. Anatomy of a PHP Monad

Algebraic Data Types

  1. Products
  2. Product Types in PHP
  3. Coproducts
  4. Coproducts in PHP
  5. Part Three

Handling Null

  1. The Problem
  2. The Example
  3. The Category Theory Solution
  4. The Programming Solution
  5. An Alternate Implementation
  6. A Note on Types

Either/Or and Error Handling

  1. The Problem
  2. The Example
  3. The Category Theory Solution
  4. The Programming Solution
  5. Either Either?

Tracking Data

  1. The Problem
  2. The Example
  3. The Category Theory Solution
  4. The Programming Solution

Combining Monadic Behavior

  1. The Example
  2. The General Approach
  3. Binding
  4. The Lexers
  5. Testing
  6. Analysis
  7. Being Lazy
  8. Part Four

Features of Functional Languages

  1. Auto-Currying
  2. Recursion Optimization
  3. Isomorphic Optimization
  4. Function Composition
  5. Function Naming
  6. Generics
  7. Callable Types
  8. Comprehensions
  9. Integrated Optionals
  10. Optional Chaining
  11. A Native Bind Operator
  12. Enumerated Types

When to Go Functional

  1. Avoid Global State
  2. Make Pure Functions
  3. Isolate IO
  4. List Application
  5. Embrace Value Objects
  6. Embrace Functions as Values
  7. Make Single-Method Callable Services
  8. Think in Terms of Pipelines and Data Flow
  9. Use Maybe and Either for Error Handling
  10. Don’t Overdo It

Further Resources

  1. Books
  2. Videos
  3. Papers

Get the free sample chapters

Click the buttons to get the free sample in PDF or EPUB, or read the sample online here

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 earned over $14 million writing, 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