Transient Mutation

Learn about Transient Mutation and its supporting packages in PHP.

Overview

When a function produces an immutable result, it’s always possible that it modifies an external state in the process. This is especially visible in value objects and persistent data structures where artifacts, such as iterative control structures, create multiple encapsulated data structures before producing a single result. Transients allow for the exertion of control in situations where some form of mutation is common and offer a non-trivial performance benefit in languages like Clojure. Using the principle of transient mutation, it’s possible to mimic the concept for PHP.

The TransientMutator trait

Because the purpose of a transient is to control mutation, its internal structure should contain procedures for checking whether a mutation has occurred and for producing a new copy of an object’s state.

The transient trait in in the bingo-functional library

An example of the TransientMutator trait in the bingo-functional library is shown below:

Get hands-on with 1200+ tech skills courses.