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

Writer monad

If you remember, pure functions cannot have any side effects, meaning you cannot put a debug statement in them, for example. If you are like me, the var_dump method is your debugging tool of choice, so you are left with breaking your purity rule or using some other debugging techniques. Since all outputs of a function must go through its return value, one of the first ideas that comes to mind is to return a tuple of values-the original return value and any kind of debug statement you need.

This solution is, however, pretty complex to put in place. Imagine you have a function that halves a numerical value which returns the halved value and the received input for debugging purposes. Now, if you want to compose this function by itself ...

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