O'Reilly logo

Functional PHP by Gilles Crettenand

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Monoids

Now that we have an understanding of applicative functors, we need to add one last piece to the puzzle before speaking about monads, the monoid. Once again, the concept is taken from the mathematical field of category theory.

A monoid is a combination of any type and a binary operation on this type with an associated identity element. For example, here are some combinations that you probably never expected were monoids:

  • Integer number and the addition operation, the identity being 0 because$i + 0 == $i
  • Integer number and the multiplication operation, the identity being 1 because$i * 1 == $i
  • Arrays and the merge operation, the identity being the empty array because array_merge($a, []) == $a
  • String and the concatenate operation, the identity ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required