I wonder what patterns are there for handling errors in PHP?
I'm close to just make an global ErrorManager object which has methods to add messages. As my entire app gets compiled, any occuring error is logged to that ErrorManager object. At the end of my Front Controller (the last few lines of the script) I tell the ErrorManager to display errors (if any). One fancy thing I thought about: Visitors really don't care about technical details. Instead they should receive an beautiful error page with a big "SORRY" and an info that the error was reported. Then, the ErrorManager sends an E-Mail to the admin with all available information and logs that stuff to a error log file. This file can be seen through the backend, to make development easy.
No idea if this is a good strategy. I bet you know better ones :-)
By default, PHP sends an error log to the server's logging system or a file, depending on how the error_log configuration is set in the php. ini file. By using the error_log() function you can send error logs to a specified file or a remote destination.
There are three (3) types of fatal errors: Startup fatal error (when the system can't run the code at installation) Compile time fatal error (when a programmer tries to use nonexistent data) Runtime fatal error (happens while the program is running, causing the code to stop working completely)
PHP error handling keywordsThrow: The throw keyword is used to signal the occurrence of a PHP exception. The PHP runtime will then try to find a catch statement to handle the exception. Catch: This block of code will be called only if an exception occurs within the try code block.
The default error handling in PHP is very simple. An error message with filename, line number and a message describing the error is sent to the browser. Exceptions are used to change the normal flow of a script if a specified error occurs. This can be done using PHP die() Function.
PHP Manual
In General
In MVC (ex. ZF)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With