r/PHP • • Sep 08 '20

Article Performance impact of PHP Exceptions

https://php.watch/articles/php-exception-performance
32 Upvotes

44 comments sorted by

View all comments

Show parent comment

2

u/gnarlyquack Sep 11 '20

Hell, can't you just use Exception directly, if you were only going to wrap a String anyway?

Well, this is more my point than the actual physical labor of typing extends \Exception (although even that can be problematic if for some reason you're already stuck in an inheritance hierarchy, although then you probably have larger problems). Presumably, an Error implementation would be relatively lightweight, possibly even just a value wrapper (and thus potentially compilable away). Being shackled to an exception means we bring along all the extra baggage of that interface: I'd like to just return a string, but now I have to bring along a file name, line number, backtrace, etc. Hardly lightweight.

The only concern is still that they may not use the returned result at all, in which case the failure has become silent.

This doesn't seem like something the type system can help you with though. At this point you would want a language feature (typically found in more functional languages) where return values can't just be ignored.

Although now we're walking down the larger road of API design. I typically have API documentation pulled up for reference, so I don't really have a problem with documenting "returns x on success or y on failure" and expecting the user to handle the result appropriately. Granted, we all make mistakes, so I'm with you that ideally we want our implementation as foolproof as possible. It seems many -- most? -- languages at some level don't really provide an elegant means to do this.

Anyway, I at least see where you're coming from, specifically in regards to PHP. I can't say that I necessarily agree with your conclusion, but I can appreciate how you got there. Thanks for the discussion.