define('C', 1); set_error_handler(function($errno, $errstr) { printf("%u: %s\n", $errno, $errstr); }); with_error_handler(function() { $a[1]; define('C', 2); }, function($errno, $errstr) { if ($errno != 8 || $errstr != 'Constant C already defined') return FALSE; }, E_NOTICE); function with_error_handler($callback, $error_handler, $error_types = E_ALL | E_STRICT) { $prv_error_handler = set_error_handler(function() use ($error_handler, &$prv_error_handler) { $r = call_user_func_array($error_handler, func_get_args()); return $r === FALSE && $prv_error_handler ? call_user_func_array($prv_error_handler, func_get_args()) : ($r === FALSE && ! $prv_error_handler ? FALSE : NULL); }); call_user_func($callback); set_error_handler($prv_error_handler); }