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.

philpalmieri

May 03, 2019
Tweet

More Decks by philpalmieri

Other Decks in Programming

Transcript

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

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

    built using Interfaces, Classes, and Inheritance Looks For: Logical Order, Hierarchy, and Representation
  3. 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
  4. 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
  5. Changing the Approach OOP Please forgive my pseudo code These

    are potentially unreliable… and harder to mock (passed in by reference?)
  6. Thinking in FP Think small. Reusable small ‘black boxes’ that

    return testable values passed to the next ‘black box’
  7. 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
  8. 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
  9. Mutants are Untrustworthy and Unpredictable Caution: Untrustworthy Mutant!! I do

    what I want What if something else changes $subTotal?
  10. 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.
  11. Diving Deeper : Lazy and Eager Loading Array ( [0]

    => 2 [1] => 4 [2] => 6 [3] => 8 [4] => 10 ) ‘Borrowed’ from: Packt Publishing FP PHP7
  12. 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.
  13. 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