The PHP podcast where everyone chimes in.

Originally aired on

September 8th, 2014

002: Functional Programming In PHP

We explore what functional programming looks like in PHP. We also touch on non-blocking, asynchronous, & event-driven concepts.


Functional Programming, Non-Blocking, Asynchronous & Event-Driven In PHP Show Summary

  • Sammy's B.S. in Linguistics does not help him pronounce Jeremy's last name properly.
  • How to wrap our heads around functional programming:
    • "Functional programming is to imperative programming as a curve is to a derivative."
    • There is no such thing as a point on a curve, but there are approximations.
    • "State" doesn't exist in real life so we don't use it in functional programming
  • SammyK has amazing show notes.
  • What is state?
    • State is holding data around in memory
  • Mutable vs Immutable
    • Mutable: can be changed
    • Immutable: cannot be changed
  • Unix piping in the command line is an example of functional programming
  • Closures are anonymous functions that can be assigned to a variable
  • Functional programming
    • Functional programming can be done with an imperative programming language like PHP, but there are native functional programming languages like Haskell.
    • State happens in a very small context
  • "Functional Programming in PHP" by Patkos Csaba says there are three guidelines to functional programming in PHP: 1. No assignments., 2. No mutable state., and 3. No while or for loops.
  • Glen mentions referential transparency and minds explode.
  • Persistent data sources get a "pass" in functional programming since they store state.
  • Immutable values
    • Immutable values make testing easier.
    • DateTime has an immutable version.
    • Primitives in PHP are essentially value objects.
    • To change the state of an immutable object, create a new one.
  • Recursion
    • Recursion
      • Recursion is used instead of while or for loops in functional programming.
  • We take a PHP elePHPant break.
  • Mojolicious has a Failraptor.
  • ReactPHP is an event-driven, non-blocking I/O library.
  • Non-blocking I/O vs multi-threading
  • Examples of using non-blocking I/O: q web server, a web scrapper & other apps that use other web protocols (not just HTTP) like Ratchet.
  • PHP is a great language to run from the command line.
  • Some discussion on how single-threaded languages make use of an event loop to perform non-blocking I/O.
  • What we use ReactPHP for:
    • Jeremy uses it to communicate with child processes in the MongoDB PHP diver test suite.
    • Most common uses are web socket apps.
    • It's possible to use it for MySQL driver communications.
  • In PHP 5.6, the PostgreSQL driver supports asynchronous connections and queries.
  • We talk about how ReactPHP can help us program functionally in PHP.
  • Igor has some great functionally programmed repos.
  • Something old, something new, something borrowed, something deprecated
    • Old & Awesome: PHP streams (again lol)
    • New & Cool: Anything off of Nikita Popov's blog.
    • Borrowed: PHP steals pretty much everything
    • Deprecated: Pear


Jeremy Mikola

Chris Boden

Glen Hinkle

Kyle Schatzle