1. Go to this page and download the library: Download josantonius/error-handler library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
josantonius / error-handler example snippets
public function getFile(): string;
public function getLevel(): int;
public function getLine(): int;
public function getMessage(): string;
public function getName(): string;
public function getFile(): string;
public function getLevel(): int;
public function getLine(): int;
public function getMessage(): string;
public function getName(): string;
/**
* The errors will be thrown from the ErrorException instance.
*
* @param int[] $errorLevel Define the specific error levels that will become exceptions.
*
* @throws WrongErrorLevelException if error level is not valid.
*
* @see https://www.php.net/manual/en/errorfunc.constants.php to view available error levels.
*/
public function convertToExceptions(int ...$errorLevel): ErrorHandler;
/**
* The errors will be thrown from the ErrorException instance.
*
* @param int[] $errorLevel Define the specific error levels that will become exceptions.
*
* @throws WrongErrorLevelException if error level is not valid.
*
* @see https://www.php.net/manual/en/errorfunc.constants.php to view available error levels.
*/
public function convertToExceptionsExcept(int ...$errorLevel): ErrorHandler;
/**
* The error handler will receive the ErrorHandled object.
*
* @see https://www.php.net/manual/en/functions.first_class_callable_syntax.php
*/
public function register(callable $callback): ErrorHandler;
/**
* If the setting value in error_reporting() is used to determine which errors are handled.
*
* If this method is not used, all errors will be sent to the handler.
*
* @see https://www.php.net/manual/en/function.error-reporting.php
*/
public function useErrorReportingLevel(): ErrorHandler;
use Josantonius\ErrorHandler\Exceptions\WrongErrorLevelException;
use Josantonius\ErrorHandler\ErrorHandler;
$errorHandler = new ErrorHandler();
$errorHandler->convertToExceptions();
// All errors will be converted to exceptions.
use Josantonius\ErrorHandler\ErrorHandler;
$errorHandler = new ErrorHandler();
$errorHandler->convertToExceptions(E_USER_ERROR, E_USER_WARNING);
// Only E_USER_ERROR and E_USER_WARNING will be converted to exceptions.
use Josantonius\ErrorHandler\ErrorHandler;
$errorHandler = new ErrorHandler();
$errorHandler->convertToExceptionsExcept(E_USER_DEPRECATED, E_USER_NOTICE);
// All errors except E_USER_DEPRECATED and E_USER_NOTICE will be converted to exceptions.
use Josantonius\ErrorHandler\ErrorHandler;
error_reporting(E_USER_ERROR);
$errorHandler = new ErrorHandler();
$errorHandler->convertToExceptions()->useErrorReportingLevel();
// Only E_USER_ERROR will be converted to exception.
use ErrorException;
use Josantonius\ErrorHandler\ErrorHandler;
set_exception_handler(function (ErrorException $exception) {
var_dump([
'level' => $exception->getLevel(),
'message' => $exception->getMessage(),
'file' => $exception->getFile(),
'line' => $exception->getLine(),
'name' => $exception->getName(),
]);
});
$errorHandler = new ErrorHandler();
$errorHandler->convertToExceptions();
// All errors will be converted to exceptions.
use Josantonius\ErrorHandler\ErrorHandled;
use Josantonius\ErrorHandler\ErrorHandler;
function handler(Errorhandled $errorHandled): void {
var_dump([
'level' => $errorHandled->getLevel(),
'message' => $errorHandled->getMessage(),
'file' => $errorHandled->getFile(),
'line' => $errorHandled->getLine(),
'name' => $errorHandled->getName(),
]);
}
$errorHandler = new ErrorHandler();
$errorHandler->register(
callback: handler(...)
);
// All errors will be converted to exceptions.
use Josantonius\ErrorHandler\ErrorHandled;
use Josantonius\ErrorHandler\ErrorHandler;
class Handler {
public static function errors(Errorhandled $exception): void { /* do something */ }
}
$errorHandler = new ErrorHandler();
$errorHandler->register(
callback: Handler::errors(...)
)->convertToExceptions();
// The error will be sent to the error handler and then throw the exception.
error_reporting(E_USER_ERROR);
class Handler {
public function errors(Errorhandled $exception): void { /* do something */ }
}
$handler = new Handler();
$errorHandled->register(
callback: $handler->errors(...),
)->convertToExceptions()->useErrorReportingLevel();
// Only E_USER_ERROR will be passed to the handler and converted to exception.
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.