Immutability

What’s immutability?

Anything considered immutability means that something does not change. To think that the state in an FP-driven application doesn’t change is overly simplistic and baseless. State does change, but it does so gracefully. The idea of immutability extends beyond assigning a value to a variable just once. It requires a vigilant discipline, which comes from avoiding the recurring assignments, which is common in imperative style programming. Sadly, PHP has neither the kind of immutable data structures present in functional languages such as OCaml, nor the kind of variables that define languages like Haskell and Elm. The explicit expressions of immutability in PHP are constants and immutable data structures such as immutable lists, tries, and value objects.

Constants in PHP

There exist two ways to define constants in PHP:

  • The const keyword
  • The define syntax

Note: PHP PSR coding standards dictate that the names of constants should be in uppercase and separated by underscores.

Get hands-on with 1200+ tech skills courses.