PHP_Exceptionizer: Convert E_NOTICE to Exception
A very simple but useful PHP_Exceptionizer library allows you to convert notices (E_NOTICE), warnings (E_WARNING), etc. to PHP exceptions.
The library is convenient to use when developing and debugging. As you know, it is better to carry it out in the error_reporting = E_ALL mode and keeping in mind that even the slightest note in the development is a hint of a fatal error.
A typical example where notices can be lost are scripts that generate a redirect to another page. This occurs when output stream buffering is enabled (ob_start ()).
Of course, on a “battle” server it is better not to convert notices into exceptions (so as not to make an elephant out of a fly), but simply read them in the log files.
However, there is a case where it makes sense to include the library in production mode. This is a mailing list generation system. Imagine that you wrote a script that sends letters to several million site subscribers, and the text of the message is generated dynamically, depending on the profile of the user or his friends. You can catch yourself thinking that running this script is very scary ... Suddenly some kind of error will happen there, and a million people will see empty fields or a “crumpled” letter? PHP_Exceptionizer can significantly reduce this risk if you enable it at the time the message is generated.
You can download the library here: dklab.ru/lib/PHP_Exceptionizer
// Somewhere in the initial script initialization code. error_reporting (E_ALL); if () { $ exceptionizer = new PHP_Exceptionizer (E_ALL); // And leave this variable so that it is not deleted until the end // script. Deleting a variable will cause PHP_Exceptionizer to be disabled. } ... // Next, you can catch notices as exceptions: try { echo $ undefinedVariable; } catch (E_NOTICE $ e) { echo "Notice raised:". $ e-> getMessage (); } ... // If you catch E_WARNING, then you will catch E_NOTICE too: try { echo $ undefinedVariable; } catch (E_WARNING $ e) { echo "Warning or better raised:". $ e-> getMessage (); } ... // And you can not catch it, then the note will cause the end of the program. echo $ undefinedVariable;
The library is convenient to use when developing and debugging. As you know, it is better to carry it out in the error_reporting = E_ALL mode and keeping in mind that even the slightest note in the development is a hint of a fatal error.
A typical example where notices can be lost are scripts that generate a redirect to another page. This occurs when output stream buffering is enabled (ob_start ()).
Of course, on a “battle” server it is better not to convert notices into exceptions (so as not to make an elephant out of a fly), but simply read them in the log files.
However, there is a case where it makes sense to include the library in production mode. This is a mailing list generation system. Imagine that you wrote a script that sends letters to several million site subscribers, and the text of the message is generated dynamically, depending on the profile of the user or his friends. You can catch yourself thinking that running this script is very scary ... Suddenly some kind of error will happen there, and a million people will see empty fields or a “crumpled” letter? PHP_Exceptionizer can significantly reduce this risk if you enable it at the time the message is generated.
You can download the library here: dklab.ru/lib/PHP_Exceptionizer