Michael Nitschinger have written a journey on avoiding nulls in PHP, about avoiding nulls completely in your PHP application. Inspired by Scala and Google Guava, the post shows you how to do it in PHP. Michael defined the problem :

While every developer has kind of accepted their existence, they are suddenly there when we’d desperately need them to not show up. How often did you writeif($obj === null) in your PHP code? Can’t there be a better, more elegant and fault-tolerant solution to the problem?

There are many solutions definantly, but Michael proposed a simple implementation which is basically two classes “Absent” and “Present” which are extended from “Optional” class pattern and represents the state where reference is present or not.

As you can see, the adoption of the Optional pattern can greatly increase the expressiveness of your code, by consequently avoiding null and its associated pitfalls. It also makes your code less error-prone, because you can’t stumble upon nulls when using this pattern consequently.