Search code examples
phperror-reporting

E_NOTICE ?== E_DEBUG, avoiding isset() and @ with more sophisticated error_handler


Which better ways exist to avoid an abundance of isset() in the application logic, and retain the ability to see debug messages (E_NOTICE) when required?

Presumption first: E_NOTICE is not an error, it's a misnomer and should actually be E_DEBUG. However while this is true for unset variables (PHP is still a scripting language), some file system functions etc. throw them too. Hence it's desirable to develop with E_NOTICEs on.

Yet not all debug notices are useful, which is why it's a common (unfortunate) PHP idiom to introduce isset() and @ throughout the application logic. There are certainly many valid use cases for isset/empty, yet overall it seems syntactic salt and can actually obstruct debugging.

That's why I currently use an error_reporting bookmarklet and a dumb on/off switch:

// javascript:(function(){document.cookie=(document.cookie.match(/error_reporting=1/)?'error_reporting=0':'error_reporting=1')})()

if (($_SERVER["REMOTE_ADDR"] == "127.0.0.1")
    and $_COOKIE["error_reporting"])
{
    error_reporting(E_ALL|E_STRICT);
}
else {/* less */}

However that still leaves me with the problem of having too many notices to search through once enabled. As workaround I could utilize the @ error suppression operator. Unlike isset() it does not completely kill debugging options, because a custom error handler could still receive suppressed E_NOTICEs. So it might help to separate expected debug notices from potential issues.

Yet that's likewise unsatisfactory. Hence the question. Does anyone use or know of a more sophisticated PHP error handler. I'm imagining something that:

  • outputs unfiltered errors/warnings/notices (with CSS absolute positioning?)
  • and AJAX-whatnot to allow client-side inspection and suppression
  • but also saves away a filtering list of expected and "approved" notices or warnings.

Surely some framework must already have a user error handler like that.

  • Basically I'm interested in warning / notice management.
  • Full E_NOTICE supression is really not desired.
  • E_NOTICES are wanted. Just less of them. Per default highlight the ones I might care about, not the expected.
  • If I run without ?order= parameter, an expected NOTICE occours. Which due to be expected I do not need to informed about more than once.
  • However when in full debugging mode, I do wish to see the presence of undefined variables through the presence (or more interestingly absence) of said debug notices. -> That's what I think they are for. Avoiding isset brings language-implicit print statements.
  • Also realize this is about use cases where ordinary PHP form handling semantics are suitable, not application areas where strictness is a must.

Oh my, someone please help rewrite this. Lengthy explanation fail.


Solution

  • Well, if you wait for PHP 7, you'll have access to the null coalesce ternary operator, which, in addition to having the coolest operator name in existence (I'm naming my next kid "Null Coalesce") will let you do this:

    $var = $some_array[$some_value] ?? "default value";
    

    Which replaces the ubiquitous (and ugly)

    $var = isset( $some_array[$some_value] ) ? $some_array[$some_value] : "default_value";