Re: [RFC] Nullsafe calls
| From: | Christoph Becker | Date: | Thu, 11 Dec 2014 00:12:55 +0000 |
| Subject: | Re: [RFC] Nullsafe calls | ||
| References: | 1 2 3 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-79551@lists.php.net to get a copy of this message | ||
Josh Watzman wrote:
> However, for a lot of failures, I don't feel that exceptions are
> appropriate. I tend to only use them for exceptional behavior --
> usually, some failure that needs to be propagated up a few levels up
> the stack, to an appropriate error boundary. This doesn't necessarily
> mean a completely unrecoverable error, but it's *locally*
> unrecoverable. For things that are completely recoverable, returning
> null might be more appropriate and more lightweight, and then this
> nullsafe operator would be useful at that point.
FWIW, there is a third option: instead of returning null, return a null
object[1]. That causes (slightly) more overhead, but it's quite
flexible and works rather nicely without requiring the (IMO) ugly ?->
nor exception handling.
Consider a non-existent user "foo":
User::find('foo')?->getName() // returns null
User::find('foo')->getName() // could also return '' or 'Unknown
user'
[1] <http://en.wikipedia.org/wiki/Null_Object_pattern>
--
Christoph M. Becker