Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Functional Programming in PHP

Functional Programming in PHP

OOP is awesome, and most PHP is written that way, but that doesn’t mean we can’t learn good habits and practices from Functional Programming too.

Learn about PHP’s ability to do some FP, how to integrate it into your stack for cleaner code, and more reliable programming. See how we can limit mutations and side effects in your current setup by making small adjustments to your class structures and methods.

Functional Programming can make your code safer, easier to test, and way more confusing – let’s find out how.

3de614918b5182f2562bc3659de0b296?s=128

philpalmieri

May 03, 2019
Tweet

Transcript

  1. Functional PHP @philpalmieri

  2. Hello My name is Phil Palmieri • PHP for ~20

    years • Node for ~5 years • Working on government stuff with
  3. What is Object Oriented Programming Imperative, Structure, and State Based

    built using Interfaces, Classes, and Inheritance Looks For: Logical Order, Hierarchy, and Representation
  4. Thinking in OOP What are the ‘Things’ and How do

    I represent them
  5. What is Functional Programming Declarative Software built using ‘Pure Functions’

    Avoids: Shared State, Mutable Data, and Side Effects
  6. Thinking in FP ‘What am I doing’ not ‘How do

    I build it’
  7. Changing The Approach Imperative Approach • I need A Cart

    • The Cart need a Customer • The Cart needs Products • Products need Variations • It should return a subtotal
  8. Changing The Approach Declarative Approach • I should be able

    to add products to a list (cart) • I should be able to associate a list with a customer • I should be able to total a given list of products • I should be able to update a product on the list
  9. Simple Example Imperative Declarative

  10. OOP vs FP Purism Real World

  11. Changing the Approach OOP Please forgive my pseudo code These

    are potentially unreliable… and harder to mock (passed in by reference?)
  12. Changing the Approach FP

  13. there is no Cart But… It’s OK if there is!

  14. Thinking in FP Think small. Reusable small ‘black boxes’ that

    return testable values passed to the next ‘black box’
  15. Thinking in FP Do one thing, and do it well

    (tested) do_one_thing($source1, $source2); This should always work because it does not rely on external influences
  16. Avoid External Players

  17. Avoid External Players

  18. Thinking in FP Avoid mutations by limiting scope. Counters and

    loops are a smell test… $x = 0 and $i++ These mutate external code and cause side effects
  19. Mutants are Untrustworthy and Unpredictable Caution: Untrustworthy Mutant!! I do

    what I want What if something else changes $subTotal?
  20. Using FP No External Dependencies No External Side-Effects Easily Testable

    without mocks
  21. Oh, yeah… Higher Order Functions A higher order function is

    a function that takes a function as an argument, or returns a function. Higher order function is in contrast to first order functions, which don't take a function as an argument or return a function as output.
  22. You are already using some FP (sort of)

  23. Diving Deeper : Lazy and Eager Loading ‘Borrowed’ from: Packt

    Publishing FP PHP7
  24. Diving Deeper : Lazy and Eager Loading Array ( [0]

    => 2 [1] => 4 [2] => 6 [3] => 8 [4] => 10 ) ‘Borrowed’ from: Packt Publishing FP PHP7
  25. Diving Deeper Currying Savvy? The technique of translating the evaluation

    of a function that takes multiple arguments into evaluating a sequence of functions, each with a single argument.
  26. Diving Deeper Currying - A Useless Example Array ( [0]

    => 6 [1] => 7 )
  27. Ok.. Now What? Today: New code should avoid mutations, and

    be easily tested Later: Start using Higher Order Functions to reduce duplications Some Day: Write an app without classes
  28. Thank You! Phil Palmieri @philpalmieri philpalmieri.me Recommended Reading