After so much time developing in PHP, I have become curious as to why the default error settings in PHP up until PHP7 were acceptable.
The default error handling regarding errors and warnings is to print to STDERR and continue as if nothing happened. Why is this considered proper? How do the developers of PHP come to this conclusion? Notices and warnings always mean SOMETHING, usually that state has be corrupted in some fashion.
I believe this creates confusion in the eyes of developers for no reason. Consider the following posts on the stacks:
Should PHP 'Notices' be reported and fixed?
Turn off warnings and errors on php/mysql
How do I turn off PHP Notices?
Notices and warnings should be converted into exception objects so that you can act upon what they mean, rather than let new developers be led to believe that they can be safely ignored.
Consider the following method:
//This method is the default error handler for the entire application
//It throws an exception instead of an error
//$level = Error level that the system is throwing (Warning,Notice,All,ect)
//$message = Error message that the server is passing
//$file = File in which the error occured
//$line = Line in which the error occured
//array $context = Array of variables that were available in the scope of the error
public function ErrorHandler($level,$message,$file,$line,$context)
{
switch($level)
{
//throw a PHPNoticeException on notices
case E_NOTICE:
throw new PHPNoticeException($message,$level);
break;
//Throw a PHPWarningException on warnings
case E_WARNING:
throw new PHPWarningException($message,$level);
break;
//Throw PHPExceptions on everything else
default:
throw new PHPException($message,$level);
}
}
Utilizing the above, one can catch and handle errors and notices. However, in their default state, a programmer is unable to act upon any warnings or notices that occur, or even know that they occurred in the first place.
Stating my questions again, why are silent errors the default behavior? Why was this decision made by the developers of PHP? It leads developers to believe that notices and warnings are unimportant. I think this to be irresponsible and reprehensible.