The PHP podcast where everyone chimes in.

Originally aired on

November 24th, 2015

035: Immutable PHP

Immutability plays a huge role in functional programming and many languages support immutability directly; like the readonly keyword in C#. It is possible to create immutable objects in PHP, but the language lacks inherent immutable features for scalar variables and class properties. We discuss how to bring functional programming concepts to PHP and brainstorm some features that could possibly be added to future versions of PHP to offer better immutability support.

with


Immutability and functional concepts in PHP Show Summary


Intro

State

  • State is a representation of something that exists in the real world
  • State is not a bad thing, but we should think in terms of flow rather than in terms of state
  • Mutable state makes time a (hidden) input to everything - the order things happen in becomes important
  • Abstracting the notion of time means that order is not important and it is easier to reason about individual components of a system
  • Where possible, we do not want to consider time. Immutability can help us with this
  • "O-O languages deal with state by hiding it inside objects. Functional languages deal with state by implicitly defining it as part of a function call"

An example demonstrating the problems with state - Zend framework test suite:

  • They used to run their whole test suite as one job
  • Some of those tests made changes to PHP's superglobals (e.g: $_SESSION)
  • This made the outcome of one test potentially depend on other tests
  • Now they run all of their tests in isolation

Functional programming vs imperative programming

  • Imperative languages (inc. O-O) execute a sequence of statements in order, modifying state
  • Functional programming defines relationships or transformations between inputs and outputs
  • An imperative language focusses on the values themselves, whereas a functional language focuses on the relationships between them
  • PHP is an imperative language, but we can apply some FP concepts. See Functional programming in PHP and Functional PHP

Immutability in PSR-7

  • The specification says that any change to a message is a change to the value of the message
  • This means that any change to a message object should create a new value/object (as opposed to modifying the existing object)
  • This makes it a candidate for use of immutable objects
  • Introducing immutability into the implementation of PSR-7 removes a lot of problems (and a lot of code)

Performance

  • Performance is sometimes cited as a reason not to use immutable variables
  • Because we can't directly change an immutable variable, we must frequently discard a variable and create a new instance
  • Benchmarks have shown that the performance hit is marginal
  • PHP is very efficient at cloning objects/variables

Database objects/ORMs

  • We should separate read and write contexts
  • Data reading and data writing should be done with different object types
  • SQL already does this:
    • The result of a SELECT statement is immutable
    • An UPDATE or INSERT statement is an entirely different concept

How could we implement immutable concepts in PHP?

  • We can never implement true immutability in PHP because reflection and closure binding allow us to break scope
  • Service objects should be completely immutable
  • For immutability to be useful, objects should not be 'locked' at construction time - we need the ability to easily create a new object based on an existing immutable object
  • Value objects should be implemented by replacing setter methods with a with statement, not providing objects that only have getter methods
  • PHP needs to decide what it wants to be:
    • Languages like Haskell or Clojure can do certain things because everything is immutable - PHP can never achieve this
    • Should PHP make architectural changes to support immutability, or accept that this is not the direction PHP should go in?
    • Is it better to build tools in user space that support immutability (and make changes to core that support them)?
    • Should PHP focus on embracing FP concepts in other ways (e.g: array_map() for iterable objects)?
  • readonly properties rfc

Final thoughts - how can developers start using immutable and FP concepts in PHP now?

  • If you need to change something inside a function, ensure this is based on the input to that function
  • Try to eliminate temporary variables inside methods - this will force you into writing smaller methods and using less mutable variables
  • Never change the input variables to a function directly
  • Use message passing rather than state
  • Separate service objects and value objects

Larry Garfield


Matthew Weier O’Phinney


Glen Hinkle


Developer Shout-Out

The Developer Shout-Out recognizes developers in the community for their contributions.

For this episode the panel guests, Larry, Matthew Weier, and Glen nominated Fabien Potencier for the Developer Shout-Out segment.

Thank you, Fabien Potencier for all your contributions to the PHP community. A $50 Amazon gift card is on its way to you.

$50 Amazon gift card sponsored by Laracasts

Laracasts

It's like Netflix for developers.

Show Notes Credit

Chris Shaw

Thank you Chris Shaw for authoring the show notes for this episode!

If you'd like to contribute show notes and totally get credit for it, check out the show-notes repo!