I know this is an old question before post this new question I checked relevant answers : answer my requirements :
I want php report any error I want my code to be able to catch and handle all errors and exceptions
in the snippet I set up handlers for any error and exception plus for shutdown I run it with PHP Version 7.0.29
<?php
class A{
public function __construct()
{
}
}
class B{
public function __construct()
{
}
public function ExceptionCatched()
{
try
{
$x=1;
$y=0;
$z= $x/$y;
}
catch (Exception $any)
{
print "Exception catched in class B msg=".$any->getMessage()."<br>";
die("completed");
}
}
}
/* this function should be invoked when the script completed
or whe error occurs
*/
function myShutDownHandler() {
print "myShutDownHandler invoked<br>";
$errfile = "unknown file";
$errstr = "shutdown";
$errno = E_CORE_ERROR;
$errline = 0;
$error = error_get_last();
if( $error !== NULL) {
//invoked by error
$errno = $error["type"];
$errfile = $error["file"];
$errline = $error["line"];
$errstr = $error["message"];
$s="***shutdown with error<br>";
$s.="type:$errno<br>";
$s.="file:$errfile<br>";
$s.="line:$errline<br>";
$s.="str:$errstr<br>";
}
else {
//imvoked before script terminated
$s = "normal shutdown without errors";
}
print $s;
die("<hr>");
}
/*this function would be invoked for any error */
function myErrorHandler($errno, $errstr, $errfile, $errline)
{
print "error handler imvoked<br>";
switch ($errno) {
case E_USER_ERROR:
echo "<b>My ERROR</b> [$errno] $errstr<br />\n";
echo " Fatal error on line $errline in file $errfile";
echo ", PHP " . PHP_VERSION . " (" . PHP_OS . ")<br />\n";
echo "Aborting...<br />\n";
exit(1);
break;
case E_USER_WARNING:
echo "<b>My WARNING</b> [$errno] $errstr<br />\n";
break;
case E_USER_NOTICE:
echo "<b>My NOTICE</b> [$errno] $errstr<br />\n";
break;
default:
echo "Unknown error type: [$errno] $errstr<br />\n";
break;
}
die("completed<br>");
}
/* this function should be invoked on every unhandle exception*/
function myExceptionHandler($exception) {
print "myExceptionHandler invoked<br>";
echo "Uncaught exception: " , $exception->getMessage(), "\n";
print "<hr>";
}
//script code
ini_set('display_errors',0);
error_reporting(-1);
register_shutdown_function( "myShutDownHandler" );
$old_error_handler = set_error_handler("myErrorHandler");
set_exception_handler('myExceptionHandler');
?>
question uncatched exception
adding following lines in script code
$a=new A();
$b=new B();
$b->ExceptionCatched();
I expected the exception was handled in class in try/catch block
but I get this output
error handler imvoked Unknown error type: [2] Division by zero completed myShutDownHandler invoked normal shutdown without errors
question parse error not catched
adding following lines in script code
$a=new A();
$b=new B();
$b->ExceptionCatched()//semicolon omitted to force error
I got standard php error msg => Parse error: syntax error, unexpected end of file in C:\wamp\www\apocalisse\public\snippet1.php on line 112