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
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php