PHP 8 has introduced an amazing code fallback tool,
Null Safe Operator, eg:
$country = $session?->user?->getAddress()?->country;
It prevents you create a lot of comparisons of all whole object tree, Null Coalesce Operator not plays well here (PHP 7.x or Earlier) beacuse above code has an method which will throw an exception because their main class is null. Here, Null Safe Operator prevents an exception.
Well, there are some hack method to emulate this behaviour into earlier versions of PHP (<= 7.X)?
Fallback to some generic class with magic methods where ever returns null can be handful.
2
Answers
We can create a black holed class to instead of throw an exception return Null if we call an undefined method of a generic class which will acts as our fallback.
Important! This approach only works with PHP >= 7.0, i will collect info to work with 5.x soon as possible.
To emulate null safe operator, you can take inspiration from the option type. The idea is simple – you wrap the value in an object, as you suggested, and have a magic method handling. Now, the magic method will either return
$this
– e.g. the same Option instance, if this is already a null, or call the method and wrap the result in an Option, to allow further chaining.The challenge you face with PHP will be where to terminate, e.g. where to return the original value, and not the wrapper. If you can afford an explicit method call at the end of the chain, it becomes straightforward.
It would look something like (not tested, written for illustrative purposes)
So you will do: