High-Level Concepts

Banishing Loops with Functional PHP

I’ve given the talk “Banishing Loops with Functional PHP” (or “Functional Programming, it just depends on how I titled it) at least three times. But it’s still, mostly a talk. You can watch a talk I’ve given many times right here:

If you enjoy the talk, please like and subscribe on YouTube 🙂

And here are my Banish Loops with Functional PHP slides as a PDF:

Or on Slideshare:

If you prefer words, you’ll find a number of them below. Because of the time it took to record and edit this “functional programming in PHP” video, I’m a little more brief than normal below.

Three Paradigms of Programming

I think really understanding the benefits and value of writing functional-style PHP begins with understanding what I see as the three major paradigms of modern (logic or traditional) programming. (I make that clarification because “declarative programming” in things like CSS and more esoteric languages is quite different.)

These three can cohabitate within the same code base, and I’d say it’s rare that you see any PHP (or JavaScript, Python, etc) code that doesn’t in some way resemble or contain at least two of these paradigms. People often lose sight of how well these interact.

Where Most People Start: Procedural PHP Code

“Procedural” or “imperative” programming has a bit of an unearned bad reputation. This comes mostly from the fact that it’s the first and most-accessible way of writing code when you’re learning almost any language. It requires a good understanding of logic, but not need for any higher-level concepts than that. It how I wrote my first programs, and I’d guess that that is true for you as well.

In procedural code, each line gives the programming language (or computer, depending how you think) a specific directive. Define this variable, increment this variable, remake this variable, loop through this set, compare these values. These are common directions in all programs, the things that makes come “imperative” or “procedural” to me is that you don’t get much more abstract than these expression.

If you have “functions” in procedural code (which is quite common) they’re there as stored procedures. What I mean is that they’re sets of transformations or operations that you’re looking to be able to repeat. These operation can do anything: make files, delete files, read database values, write database values, or any combination and permutation of the above.

Object-Oriented Code Uses the Way Humans Think

When I look around a room, my eyes see colors and sizes. I see a silver half-circle before me. That connects to a narrow silver tube. Which connects to a different silver circle. I’m describing a desk lamp, and if that description confused you, we’re getting at the value of thinking in “objects” vs procedures. We see the whole world as “object” or “aggregates.”

We see collections of properties and behaviors. We know that’s a chair, and that means we can sit on it. We don’t even understand how we know that’s a chair and how we know we can sit on it. And it doesn’t matter. We understand and relate to the idea, and can use similar substitutes rather seamlessly. It’s super powerful.

Object-oriented programming gets complicated because we’re very familiar with physical objects, but ideas are softer. It’s hard to know if an “EmailNotifier” object makes sense in your system, or is well designed, because you’ve rarely seen. Others, whole books don’t even resolve these problems of OOP, so this article certainly won’t. So we’ll keep going…

Functional Programming Code Builds on Mathematical Functions

Similar to the way that object-oriented programming comes from the way that we understand objects, functional programming arises from mathematics and the functions we see there. (For example, y = x2.) In this way, we ignore the understanding and use of the word “function” that we have in procedural or object-oriented code. Here when we say “functions” then, we’ll often clarify that we mean “pure-functions” in the mathematical sense.

A function in a program is considered pure if it has certain properties. Most notably:

  • It uses only it’s inputs. The parameters to a pure function are the only things that constrain or control the result. Nothing else effects it, not the time, the database, the hard drive, or the number of times it’s been called.
  • It effects only its outputs. That means that it doesn’t save data, it doesn’t delete data, or anything else that could be considered a “side-effect.”

If you’re thinking “pure functions” sound pretty useless, you’re right. That’s a good and important critique. That’s a problem that goes to the heart of why functional programming isn’t a runaway success. But for us, we can skip that worry, you’ll just have to trust me.

Why Functional PHP is Helpful

What “pure functions” are great at is being easy to understand. Because they don’t depend on anything unstated, nor do that they do anything unstated, you can quickly read a pure function and know what it does.

Obviously, some familiarity with the names of functions in the system, and the format of function declaration, is necessary. But if you get that, most functions are pretty similar to y = x2 in their clarity and simplicity.

An Abrupt Ending

At this point, I ran out of energy. So use the little slide-in widget at right to signal to my your interest in the rest of this post. If enough people express, I’ll plan to finish the full text soon. It’s just more effort than I anticipated.

Express your interest:

Want David to Finish the Banish Loops talk article?

If so, drop a line here. If 10 people put their name in this box, I’ll do it within the month. (And if you explicitly don’t want me write the article, email thoughtfulcode @ davidbhayes.com, or sign-up for my email list with a different form 🤓😊)

Here’s what the video covers that the article doesn’t currently:

  • Using Functional PHP without Libraries
    • Transform a Set in PHP with array_map
    • How to Filter in Pure PHP, array_filter
    • Downsides of Pure-PHP Functional Programming
  • Using Functional PHP with a Collection Library: an example
    • What’s a Collection Library? Why Use One?
    • Example Starting Code, with some explanation
    • Example Finished Code, with some explanation
  • Why You Should Try Functional PHP (Conclusion)

2 thoughts on “Banishing Loops with Functional PHP

  1. Bob says:

    I have been programming with PHP for over 20 years, and I am just not upgrading my skill set to include functional programming (on purpose, where I have certainly done it by accident in the past) and I really appreciated this post/video.

    Thank you.

  2. Umar says:

    Hi David. This was an outstanding post about functional programming in PHP and how to effectively use Laravel collections. I think you should post more often and also do more Youtube videos. You deserve a lot more recognition for your work and more content will definitely be noticed and welcomed by the community. Please keep up the good work!

Leave a Reply

Your email address will not be published. Required fields are marked *