On Error Handling and Closures
Note: This article was originally published at phly, boy, phly
on 16 December 2011.
The error suppression operator in PHP ("@") is often seen as a necessary evil. Many, many low-level function will return a value indicating an error, but also raise an E_NOTICE or E_WARNING -- things you might be able to recover from, or conditions where you may want to raise an exception.
So, at times, you find yourself writing code like this:
if (false === ($fh = @fopen($filename, 'r'))) { throw new RuntimeException(sprintf('Could not open file "%s" to read', $filename)); }Seems straight-forward enough, right? But it's wrong on so many levels.


